17

I noticed that code below results in different messages in PHP 5.x and 7:

if ('0xFF' == 255) {
    echo 'Equal';
} else {
    echo 'Not equal';
}
  • 5.x: Equal
  • 7: Not equal

Tried to find a description of the changes that cause it in migration guide and in the PHP doc but couldn't find anything. Probably it is somewhere there and I just missed it. Can you, please, point it? Thank you!

Where I looked

Pavel
  • 3,967
  • 2
  • 29
  • 35
  • [that's exactly why I always hated languages with implicit type conversions between string/numeric/boolean etc. and type-converting comparisons](http://i.stack.imgur.com/35MpY.png) - [guess what, each language of those has its own rules, because all the other are stupid, duh](http://php.net/manual/en/types.comparisons.php) –  Sep 27 '16 at 01:30

1 Answers1

24

It's here: http://php.net/manual/en/migration70.incompatible.php

Changes to String Handling

Hexadecimal strings are no longer considered numeric

Strings containing hexadecimal numbers are no longer considered to be numeric. For example: <?php var_dump("0x123" == "291"); etc...

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 4
    And if you absolutely must do this kind of comparison, you can wrap the hex value in [hexdec](http://php.net/manual/en/function.hexdec.php) to get a decimal out. – rockerest Sep 26 '16 at 18:38
  • Ah, this page was the first I looked into, can not believe I missed it! Thank you! – Pavel Sep 26 '16 at 18:39
  • 1
    See also the motivating RFC, [Remove hex support in numeric strings](https://wiki.php.net/rfc/remove_hex_support_in_numeric_strings). – bishop Sep 27 '16 at 00:06