note 67204 deleted from language.types.boolean by danbrown
| From: | danbrown@php.net | Date: | Thu, 18 Jun 2009 14:11:36 +0000 |
| Subject: | note 67204 deleted from language.types.boolean by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157084@lists.php.net to get a copy of this message | ||
Note Submitter: jasper at jtey dot com
----
The following expressions are equivalent:
<?php
// setting true
$flag = true;
$flag = True;
$flag = TRUE;
$flag = 1==1;
// setting false
$flag = false;
$flag = False;
$flag = FALSE;
$flag = 1==2;
?>
The moral of the story is that boolean operators return a boolean value, i.e., "1==1"
returns a boolean value of true. Someone who is not aware of this may write a block of code such
as:
<?php
// even number?
$num = 10;
if($num % 2 == 0){
$isEven = true;
}
else{
$isEven = false;
}
?>
when all that is needed is:
<?php
$num = 10;
$isEven = $num % 2 == 0;
?>
Other examples, for illustrative purposes:
<?php
// two numbers
$a = 2;
$b = 3;
$aBiggerThanB = 2 > 3; // $aBiggerThanB is set to false
// lower case vowel check (corrected)
$c = "u";
$isVowel = $c == "a"|| $c == "e"|| $c == "i"|| $c == "o"||
$c == "u";
?>