When I was practicing in php, I noticed, that the following expressions yield strange results:
011 == 11 // false
'011' == 11 // true
Shouldn't they both evaluate to same result?
This is because 011
is treated as an octal value because of the leading 0
.
Here's the more in-depth explanation:
011
literal is recognized as an octal value9
9 == 11 // false
As of the '011' == 11
, it evaluates to true
, because when string is compared to integer, it's coerced to the integer value as well. Interestingly, the leading zero in the string is ignored in the proccess and the php interpreter treats the value as a decimal rather than an octal one!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With