What will be the output of each of the statements below and why?
var_dump(0123 == 123); var_dump('0123' == 123); var_dump('0123' === 123);
var_dump(0123 == 123) will output
bool(false) because the leading
0123 tells the PHP interpreter to treat the value as octal (rather than decimal) value, and 123 octal is equal to 83 decimal, so the values are not equal.
var_dump('0123' == 123) will output
bool(true) since the string
0123 will automatically be coerced to an integer when being compared with an integer value. Interestingly, when this conversion is performed, the leading
0 is ignored and the value is treated as a decimal (rather than octal) value, so the values are bother 123 (decimal) and are therefore equal.
var_dump('0123' === 123) outputs
bool(false) since it performs a more strict comparison and does not do the automatic type coercion of the string to an integer.