note 56436 added to language.operators
| From: | sm at osu1 dot php dot net | Date: | Fri, 02 Sep 2005 12:15:17 +0000 |
| Subject: | note 56436 added to language.operators | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94657@lists.php.net to get a copy of this message | ||
Note the highly unfortunate difference from Java, which associates the trinary operator
right-to-left.
---------------------------- source
function trinaryTest($foo){ // works as you think in Java, but not PHP
$bar = $foo > 20
? "greater than 20"
: $foo > 10
? "greater than 10"
: $foo > 5
? "greater than 5"
: "not worthy of consideration";
echo $foo." => ".$bar."\n";
}
echo "\n\n\n----trinaryTest\n\n";
trinaryTest(21);
trinaryTest(11);
trinaryTest(6);
trinaryTest(4);
function trinaryTestParens($foo){
$bar = $foo > 20
? "greater than 20"
: ($foo > 10
? "greater than 10"
: ($foo > 5
? "greater than 5"
: "not worthy of consideration"));
echo $foo." => ".$bar."\n";
}
echo "\n\n\n----trinaryTestParens\n\n";
trinaryTestParens(21);
trinaryTestParens(11);
trinaryTest(6);
trinaryTestParens(4);
---------------------------- output
----trinaryTest
21 => greater than 5
11 => greater than 5
6 => greater than 5
4 => not worthy of consideration
----trinaryTestParens
21 => greater than 20
11 => greater than 10
6 => greater than 5
4 => not worthy of consideration
----
Manual Page -- http://www.php.net/manual/en/language.operators.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56436
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56436&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56436&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56436&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56436&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56436&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56436&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56436&report=yes
Search -- http://master.php.net/manage/user-notes.php