note 125758 deleted from language.operators.comparison by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 17:42:00 +0000 |
| Subject: | note 125758 deleted from language.operators.comparison by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217870@lists.php.net to get a copy of this message | ||
Note Submitter: yiminrong at yahoo dot ca
----
The string equality operator has a big gotcha in that if both operators match the format
/^0+[Ee][0-9]+$/, then the values will be considered equal. All these, which represent common use
cases, will output true:
echo '0e56' == '0E31'; // comparing short hex strings
echo md5('240610708') == md5('QNKCDZO'); // comparing hashes - see comment by
Ray Paseur in md5 function
echo '000e123' == '00e0123'; // permutation of a string
The reason is that strings in the format /^0+[Ee][0-9]+$/ are interpreted as a number in scientific
notation and zero raised to any power is zero.
Unless it is certain that the string will not match the specific format, then === should be used
instead of == for strings.