0v1.ro

(zero V unu)

Compare strings in PHP

Just read a comment in the PHP documentation and had another PHP madness moment.

It seems you have to be really, really careful when comparing strings that include numbers…

 

Command \ PHP Version 7.0.0 – 7.1.3 5.4.4 – 5.6.30 5.2.1 – 5.4.3
var_dump(‘123’ == ‘       123’); TRUE TRUE TRUE
var_dump(‘1e3’ == ‘1000’); TRUE TRUE TRUE
var_dump(‘+74951112233’ == ‘74951112233’); TRUE TRUE TRUE
var_dump(‘00000020’ == ‘0000000000000000020’); TRUE TRUE TRUE
var_dump(‘0X1D’ == ’29E0′); FALSE TRUE TRUE
var_dump(‘0xafebac’ == ‘11529132’); FALSE TRUE TRUE
var_dump(‘0xafebac’ == ‘0XAFEBAC’); FALSE TRUE TRUE
var_dump(‘0xeb’ == ‘+235e-0’); FALSE TRUE TRUE
var_dump(‘0.235’ == ‘+.235’); TRUE TRUE TRUE
var_dump(‘0.2e-10’ == ‘2.0E-11’); TRUE TRUE TRUE
var_dump(‘61529519452809720693702583126814’ == ‘61529519452809720000000000000000’); FALSE FALSE TRUE

https://3v4l.org/rop5S