Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is var_dump((int)(PHP_INT_MAX + 1)) a negative number?

Tags:

php

I pulled this result from here:

And interestingly, the result of var_dump((int)(PHP_INT_MAX + 1)) will be displayed as a negative number (in the case of this specific example, it will display int(-9223372036854775808)). Again, the key here is for the candidate to know that the value will be displayed as a negative number.

Is the int negative because adding 1 will overflow the integer bits and change the bit representing the sign of the int? What's the reason?

like image 689
sunny Avatar asked Oct 19 '22 10:10

sunny


1 Answers

@kainaw is right. This is called "buffer overflow".

PHP handled it in the best way possible. If PHP didn't make that integer positive, there would be an extra "bit" floating around in the RAM (probably causing a 502 error, but it could be much worse)

like image 93
Aeron R Avatar answered Oct 28 '22 23:10

Aeron R