note 80123 deleted from language.operators.comparison by nlopess

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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.
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.