note 75317 added to language.types.boolean
| From: | SchraalhansKeukenmeester at osu1 dot php dot net | Date: | Wed, 23 May 2007 17:03:30 +0000 |
| Subject: | note 75317 added to language.types.boolean | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126850@lists.php.net to get a copy of this message | ||
Re: andy at txtnation dot com
<quote> The braces are of course optional </quote>
Nothing optional about the 'braces' here.
'( )' are parentheses. '{ }' are braces. But we get the point.
<?php
$num = 10;
$isEven = !($num % 2);
echo ($isEven) ? 'Even' : 'Odd';
//outputs : Even
$isEven = !$num % 2;
echo ($isEven) ? 'Even' : 'Odd';
//outputs : Odd (with ANY number != 0 !!)
?>
Operator precedence and implicit casts at work:
$num = 10;
!$num => (implicit cast to bool) $num: (bool) 10 = true
!true => negate true : false
false % 2 => (implicit cast to int) false : (int) false = 0
0 % 2 => remainder of 0 intdiv 2 : 0
$isEven = 0 => integer assignment : 0
($isEven) ? => (implicit cast to bool) 0 : (bool) 0 = false
echo (false) ? 'Even' : 'Odd' => condition false : 'Odd'
Wether or not PHP actually performs the (bool) casts under the hood is irrelevant to the outcome
here.
----
Server IP: 194.109.193.76
Probable Submitter: 76.79.72.82
----
Manual Page -- http://www.php.net/manual/en/language.types.boolean.php
Edit -- https://master.php.net/note/edit/75317
Del: integrated -- https://master.php.net/note/delete/75317/integrated
Del: useless -- https://master.php.net/note/delete/75317/useless
Del: bad code -- https://master.php.net/note/delete/75317/bad+code
Del: spam -- https://master.php.net/note/delete/75317/spam
Del: non-english -- https://master.php.net/note/delete/75317/non-english
Del: in docs -- https://master.php.net/note/delete/75317/in+docs
Del: other reasons-- https://master.php.net/note/delete/75317
Reject -- https://master.php.net/note/reject/75317
Search -- https://master.php.net/manage/user-notes.php