note 80121 deleted from language.operators.comparison by nlopess
| From: | nlopess@php.net | Date: | Sun, 30 Dec 2007 14:55:59 +0000 |
| Subject: | note 80121 deleted from language.operators.comparison by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134300@lists.php.net to get a copy of this message | ||
Note Submitter: thingy dot 808 at gmail dot com
----
Try out the following, to show inconsistencies between nested ternary comparisons:
<?
function bogsdollocks($num) {
echo "Number input = ".$num."<br>";
//What's up with the following -
echo ($num == 7) ? "exactly seven" : ($num > 7) ? "more than seven" :
"less than seven" ;
echo "<br>";
// after much monkeying around the following is ok, note the brackets around the latter nested
ternary statement -
echo ($num == 7) ? "TERNARY OK : exactly seven" : (($num > 7) ? "TERNARY OK :
more than seven" : "TERNARY OK : less than seven" );
echo "<br>";
// The following is the reference which is always ok -
if ($num == 7) { echo "REFERENCE : exactly seven"; }
elseif ($num > 7) { echo "REFERENCE : more than seven";}
else { echo "REFERENCE : less than seven";}
echo "<br>";
}
echo "<HR>";
bogsdollocks(6);
echo "<HR>";
bogsdollocks(7);
echo "<HR>";
bogsdollocks(8);
echo "<HR>";
?>
Gives the results:
Number input = 6
less than seven
TERNARY OK : less than seven
REFERENCE : less than seven
---------------------------------
Number input = 7
more than seven
TERNARY OK : exactly seven
REFERENCE : exactly seven
---------------------------------
Number input = 8
more than seven
TERNARY OK : more than seven
REFERENCE : more than seven
I try to avoid ternary.