note 56572 added to language.operators.comparison

From: Date: Wed, 07 Sep 2005 20:09:44 +0000
Subject: note 56572 added to language.operators.comparison
Groups: php.notes 
Request: Send a blank email to php-notes+get-94873@lists.php.net to get a copy of this message
The following contrasts the trinary operator associativity in PHP and Java. The first test would work as expected in Java (evaluates left-to-right, associates right-to-left, like if stmnt), the second in PHP (evaluates and associates left-to-right) <?php echo "\n\n######----------- trinary operator associativity\n\n"; function trinaryTest($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 "----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 "----trinaryTestParens\n\n"; trinaryTestParens(21); trinaryTestParens(11); trinaryTest(6); trinaryTestParens(4); ?> Output: ######----------- trinary operator associativity ----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.comparison.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56572 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56572&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56572&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56572&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56572&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56572&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56572&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56572&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94873) next »