- iPhone (2)
- Learning (42)
- Linux (4)
- Debian (1)
- Mac Terminal / Linux (5)
- Other Stuff (1)
- Programming (66)
- Slim Framework (1)
- Uncategorized (4)
- WordPress (5)
We participate in the Amazon Service LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
var_dump(0123 == 123); var_dump('0123' == 123); var_dump('0123' === 123);
At first glance one might think that the leading zero on line one is simply of no value. But one would be wrong… In PHP, a leading zero in a number means that it’s an octal number.
Line one above will compare octal 123, which when converted to decimal is 83, and is not equal to 123 so the output will be false.
Line two has that octal number as a string, so we have to know how strings get converted to numbers. It will be coerced to an integer, and the leading zero will be ignored. Thus it will output true.
Line three will output false because the === operator will prevent coercion, and a string can not be identical to a number.