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.
----
Server IP: 219.94.145.73
Probable Submitter: 123.222.18.115
----
Manual Page -- http://www.php.net/manual/en/language.operators.comparison.php
Edit -- https://master.php.net/note/edit/80123
Del: integrated -- https://master.php.net/note/delete/80123/integrated
Del: useless -- https://master.php.net/note/delete/80123/useless
Del: bad code -- https://master.php.net/note/delete/80123/bad+code
Del: spam -- https://master.php.net/note/delete/80123/spam
Del: non-english -- https://master.php.net/note/delete/80123/non-english
Del: in docs -- https://master.php.net/note/delete/80123/in+docs
Del: other reasons-- https://master.php.net/note/delete/80123
Reject -- https://master.php.net/note/reject/80123
Search -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.