1

I read the manual from the php homepage

It writes:

// this doesn't go for 
//hexadecimal specified integers above 2^32-1:

var_dump( 0x100000000 );

// output: int(2147483647)

But it has 4.5 bytes which is larger than int(4 bytes), and I test it.

It outputs float.

I don't understand why they contradict?

3
  • 2
    What does echo PHP_INT_MAX; tell you? Commented Jun 14, 2012 at 19:28
  • I test in the class which is 32 and my desktop is 64 and it output 9223372036854775807 Commented Jun 14, 2012 at 19:31
  • 1
    You have not yet reached the limit. The 2^32 boundary is typical for 32 bit systems, but the actual value can vary. Commented Jun 14, 2012 at 19:42

2 Answers 2

3

From the PHP manual page on integers: "If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead."

Since the integer type is too small to represent that number, PHP is automagically converting it to a float so you don't lose data. This is expected behavior.

However, the specific example that you quoted is clearly wrong in the manual. It looks like someone made an error when writing the manual, or it may be that the behavior of oversize hexadecimal literals was changed since the time that manual page was written.

Sign up to request clarification or add additional context in comments.

Comments

0

It outputs a float for me: float(4294967296)

DEMO: http://codepad.org/otsGOiWf

2 Comments

and what is " this doesn't go for hexadecimal specified integers above 2^32-1:" mean?
Interpret the comment as "Hexadecimal-specified integers greater than PHP_INT_MAX are not interpreted as a float." However, this is no longer true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.