note 38139 deleted from language.operators.comparison by crell

From: Date: Thu, 31 Mar 2022 17:35:35 +0000
Subject: note 38139 deleted from language.operators.comparison by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-217866@lists.php.net to get a copy of this message
Note Submitter: jwhiting at hampshire dot edu ---- note: the behavior below is documented in the appendix K about type comparisons, but since it is somewhat buried i thought i should raise it here for people since it threw me for a loop until i figured it out completely. just to clarify a tricky point about the == comparison operator when dealing with strings and numbers: ('some string' == 0) returns TRUE however, ('123' == 0) returns FALSE also note that ((int) 'some string') returns 0 and ((int) '123') returns 123 the behavior makes senes but you must be careful when comparing strings to numbers, e.g. when you're comparing a request variable which you expect to be numeric. its easy to fall into the trap of: if ($_GET['myvar']==0) dosomething(); as this will dosomething() even when $_GET['myvar'] is 'some string' and clearly not the value 0 i was getting lazy with my types since php vars are so flexible, so be warned to pay attention to the details...

« previous php.notes (#217866) next »