note 110955 deleted from language.operators.comparison by aharvey

From: Date: Tue, 08 Jan 2013 02:37:28 +0000
Subject: note 110955 deleted from language.operators.comparison by aharvey
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192799@lists.php.net to get a copy of this message
Note Submitter: JMoney ---- 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.

« previous php.notes (#192799) next »