note 56572 deleted from language.operators.comparison by crell

From: Date: Wed, 30 Mar 2022 21:52:01 +0000
Subject: note 56572 deleted from language.operators.comparison by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-217796@lists.php.net to get a copy of this message
Note Submitter: ---- 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

« previous php.notes (#217796) next »