note 75317 deleted from language.types.boolean by danbrown
| From: | danbrown@php.net | Date: | Thu, 18 Jun 2009 14:11:21 +0000 |
| Subject: | note 75317 deleted from language.types.boolean by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157081@lists.php.net to get a copy of this message | ||
Note Submitter: Schraalhans Keukenmeester
----
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.