note 110955 added to language.operators.comparison

From: Date: Fri, 28 Dec 2012 18:21:57 +0000
Subject: note 110955 added to language.operators.comparison
Groups: php.notes 
Request: Send a blank email to php-notes+get-192700@lists.php.net to get a copy of this message
Be careful when comparing values with 0. If 0 is the string '0' and you are comparing it to a string, the values are not cast to numbers as you would expect. <?php // comparison of two strings var_dump('test' > '0'); // true var_dump('test' <= '0'); // false // comparison of a number to a string var_dump('test' > 0); // false var_dump('test' <= 0); // true // explicitly cast to integers $x = '0'; $y = 'test'; var_dump($y > (int)$x); // false ?> If you want to test if a value is greater than or less than another it is best to explicitly cast to integers. ---- Server IP: 69.147.83.199 Probable Submitter: 75.26.165.145 ---- Manual Page -- http://php.net/manual/en/language.operators.comparison.php Edit -- https://master.php.net/note/edit/110955 Del: integrated -- https://master.php.net/note/delete/110955/integrated Del: useless -- https://master.php.net/note/delete/110955/useless Del: bad code -- https://master.php.net/note/delete/110955/bad+code Del: spam -- https://master.php.net/note/delete/110955/spam Del: non-english -- https://master.php.net/note/delete/110955/non-english Del: in docs -- https://master.php.net/note/delete/110955/in+docs Del: other reasons-- https://master.php.net/note/delete/110955 Reject -- https://master.php.net/note/reject/110955 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#192700) next »