note 81370 added to language.operators.comparison

From: Date: Mon, 25 Feb 2008 11:13:33 +0000
Subject: note 81370 added to language.operators.comparison
Groups: php.notes 
Request: Send a blank email to php-notes+get-136469@lists.php.net to get a copy of this message
Most of the time, you may be content with your conditionals evaluating to true if they are evaluating a non-false, non-zero value. You may also like it when they evaluate to false when you use the number 0. However, there may be times where you want to make a distinction between a non-false value and a boolean true. You may also wish to make a distinction between a boolean false and a zero. The identity operator can make this distinction for you. <?php $a = 'some string'; $b = 123; $c = 0; if($a && $b && (!$c)) { echo "True.\n"; } else { echo "False.\n"; } if($a == true && $b == true && $c == false) { echo "True.\n"; } else { echo "False.\n"; } if($a === true || $b === true || $c === false) { echo "True.\n"; } else { echo "False.\n"; } ?> The above code outputs the following: True. True. False. As you can see, in the first two cases, $a and $b are considered true, while $c is considered false. If this wasn't the case, neither of the first two conditionals would have echoed "True." In the last case, I've cleverly used the || operator to demonstrate that both $a and $b do not evaluate to true with the identity operator, nor does $c evaluate to false. The === operator can be used to distinguish boolean from non-boolean values. ---- Server IP: 209.41.74.194 Probable Submitter: 71.82.169.119 ---- Manual Page -- http://www.php.net/manual/en/language.operators.comparison.php Edit -- https://master.php.net/note/edit/81370 Del: integrated -- https://master.php.net/note/delete/81370/integrated Del: useless -- https://master.php.net/note/delete/81370/useless Del: bad code -- https://master.php.net/note/delete/81370/bad+code Del: spam -- https://master.php.net/note/delete/81370/spam Del: non-english -- https://master.php.net/note/delete/81370/non-english Del: in docs -- https://master.php.net/note/delete/81370/in+docs Del: other reasons-- https://master.php.net/note/delete/81370 Reject -- https://master.php.net/note/reject/81370 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136469) next »