Why is this statement true in PHP?

✔ Recommended Answer

The result of this script:

var_dump((0x0FFFFFFF)); var_dump((0xF0FFFFFF)); var_dump((0x0FFFFFFF | 0xF0FFFFFF)); var_dump((0xFFFFFFFF)); var_dump(((0x0FFFFFFF | 0xF0FFFFFF)) != (0xFFFFFFFF));

is

int(268435455)float(4043309055)int(-1)float(4294967295)bool(true)

PHP converts hexadecimal numbers larger than 31 bits into floats, as an integer is signed, and can therefore only hold 31 positive bits.

Hexadecimal numbers are unsigned, so the conversion makes sense.

The first "or" operation converts the float into an integer, as it doesn't make sense to perform an "or" on a float. So PHP converts the float to an int for the or, the result is an int, but the next hexadecimal conversion is a float, and the values are not the same.

To convert the float to a integer in a bitwise fashion, OR it with 0x0:

var_dump((0xFFFFFFFF | 0x0)); var_dump(((0x0FFFFFFF | 0xF0FFFFFF)) != (0xFFFFFFFF | 0x0));

results in

int(-1)bool(false)

Source: stackoverflow.com

Answered By: Adam Davis

Method #2

The result of the script you provided is:

scss
int(268435455) float(4043309055) int(-1) float(4294967295) bool(true)

The var_dump() function is used to output the values of the expressions to the console.

  1. var_dump((0x0FFFFFFF)); - The value 0x0FFFFFFF represents the maximum value of a 32-bit unsigned integer in hexadecimal format. The var_dump() function outputs an integer value of 268435455 to the console.

  2. var_dump((0xF0FFFFFF)); - The value 0xF0FFFFFF represents a 32-bit integer with the most significant byte set to 0xF0. Since the value is greater than the maximum integer value in PHP, it is represented as a float. The var_dump() function outputs a float value of 4043309055 to the console.

  3. var_dump((0x0FFFFFFF | 0xF0FFFFFF)); - The bitwise OR operation between 0x0FFFFFFF and 0xF0FFFFFF results in 0xFFFFFFFF, which represents the maximum value of a 32-bit unsigned integer. The var_dump() function outputs an integer value of -1 to the console.

  4. var_dump((0xFFFFFFFF)); - The value 0xFFFFFFFF represents the maximum value of a 32-bit unsigned integer in hexadecimal format. The var_dump() function outputs a float value of 4294967295 to the console.

  5. var_dump(((0x0FFFFFFF | 0xF0FFFFFF)) != (0xFFFFFFFF)); - The expression evaluates to true because the bitwise OR operation between 0x0FFFFFFF and 0xF0FFFFFF results in 0xFFFFFFFF, which is not equal to the maximum value of a 32-bit unsigned integer. The var_dump() function outputs a boolean value of true to the console.

Comments

Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755