note 125758 added to language.operators.comparison

From: Date: Tue, 02 Feb 2021 13:51:28 +0000
Subject: note 125758 added to language.operators.comparison
Groups: php.notes 
Request: Send a blank email to php-notes+get-219197@lists.php.net to get a copy of this message
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. ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.159.166 (proxied: 2001:1970:4d21:dd00:9043:a41e:b108:6e12) ---- Manual Page -- http://php.net/manual/en/language.operators.comparison.php Edit -- https://master.php.net/note/edit/125758 Del: integrated -- https://master.php.net/note/delete/125758/integrated Del: useless -- https://master.php.net/note/delete/125758/useless Del: bad code -- https://master.php.net/note/delete/125758/bad+code Del: spam -- https://master.php.net/note/delete/125758/spam Del: non-english -- https://master.php.net/note/delete/125758/non-english Del: in docs -- https://master.php.net/note/delete/125758/in+docs Del: other reasons-- https://master.php.net/note/delete/125758 Reject -- https://master.php.net/note/reject/125758 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#219197) next »