note 56436 deleted from language.operators by danbrown
| From: | danbrown@php.net | Date: | Fri, 30 Jan 2009 20:42:19 +0000 |
| Subject: | note 56436 deleted from language.operators by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150003@lists.php.net to get a copy of this message | ||
Note Submitter: sm
----
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