note 81370 deleted from language.operators.comparison by cmb
| From: | cmb@php.net | Date: | Sun, 08 Dec 2019 11:32:53 +0000 |
| Subject: | note 81370 deleted from language.operators.comparison by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213469@lists.php.net to get a copy of this message | ||
Note Submitter: Amaroq
----
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.