note 91593 added to language.operators.comparison

From: Date: Thu, 18 Jun 2009 14:01:07 +0000
Subject: note 91593 added to language.operators.comparison
Groups: php.notes 
Request: Send a blank email to php-notes+get-157059@lists.php.net to get a copy of this message
Quote: "If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers." There are a lot of comments discussing the difference between == and ===, and how the latter does a strict comparison including type, thus allowing you to work around the behaviour described in the quote above. However, what is less obvious is that this also applies to the other operators in the list, and this can lead to unwanted behaviour. For example: <? var_dump("51" < "500"); ?> output: bool(true) This is because if both strings are numeric they are converted to numbers in order to do the comparison. Often this is what you want, but not always. For example if you want to sort an array in alphabetical order (e.g. "5", "50", "500", "51", "52", etc.) then the above will not sort correctly. If you are using a manual sort then they will be sorted numerically ("5", "50", "51", "52", etc..., "500"). More worryingly, if you use one of the sort() functions then any wholly numeric entries will be sorted numerically when compared with other numeric entries, but alphabetically when compared with strings! This leads to an undefined sort order, which may give different results based on the order the original array was sorted (i.e. which comparisons take place, and in which order) rather than the actual data in the array. The workaround is either to add a string character (the same one) to the start of both values, or to use the strcmp() function. For example: <?php // Instead of: if ($a < $b) { ... } // you could use if ("x" . $a < "x" . $b) { ... } // or if (strcmp($a, $b) < 0) { ... } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 62.56.102.23 ---- Manual Page -- http://www.php.net/manual/en/language.operators.comparison.php Edit -- https://master.php.net/note/edit/91593 Del: integrated -- https://master.php.net/note/delete/91593/integrated Del: useless -- https://master.php.net/note/delete/91593/useless Del: bad code -- https://master.php.net/note/delete/91593/bad+code Del: spam -- https://master.php.net/note/delete/91593/spam Del: non-english -- https://master.php.net/note/delete/91593/non-english Del: in docs -- https://master.php.net/note/delete/91593/in+docs Del: other reasons-- https://master.php.net/note/delete/91593 Reject -- https://master.php.net/note/reject/91593 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#157059) next »