note 95952 added to language.operators.comparison
| From: | ISAWHIM at osu1 dot php dot net | Date: | Sat, 30 Jan 2010 18:53:34 +0000 |
| Subject: | note 95952 added to language.operators.comparison | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165517@lists.php.net to get a copy of this message | ||
When it comes to formatting structure of the conditional statements, I found this to work best and
retain logic in views...
<?
$z = 2;
$text = ($z===1 ? 'ONE'
: ($z===2 ? 'TWO'
: ($z===3 ? 'THREE'
: 'MORE' )));
echo($text); // RESULT='TWO'
// LONGHAND
$z = 2;
$text = ($z===1?'ONE' : ($z===2?'TWO' : ($z===3?'THREE' :
'MORE')));
echo($text); // RESULT='TWO'
?>
Since this is expected to test logic, and nothing more, only use it to test logic.
To test order, if you ever forget...
<?
$z = 1;
$text = ($z===1 ? 'FIRST : OUTTER'
: ($z===1 ? 'SECOND : INNER'
: ($z===1 ? 'THIRD : LAST'
: 'FAIL EVAL DEFAULT' )));
echo($text); // RETURN='FIRST : OUTTER'
$z = 2;
$text = ($z===1 ? 'FIRST : OUTTER'
: ($z===1 ? 'SECOND : INNER'
: ($z===1 ? 'THIRD : LAST'
: 'FAIL EVAL DEFAULT' )));
echo($text); // RETURN='FAIL EVAL DEFAULT'
?>
(IF ? THEN : ELSE)
(IF ? THEN : ELSE(IF ? THEN : ELSE(IF ? THEN : ELSE))
That can't be read from inside to out, unlike a math formula, because the logic in comparison
is not the same. In math nesting, you need the solution to the deepest nested element first. In
logic comparison, you always start outside before you compare inside. (Logically, IF there is no
door THEN you need something ELSE to get inside. Oh, there is a window... We are inside, now IF
there is a fridge THEN open it or ELSE you starve.)
----
Server IP: 69.147.83.197
Probable Submitter: 75.92.64.47
----
Manual Page -- http://www.php.net/manual/en/language.operators.comparison.php
Edit -- https://master.php.net/note/edit/95952
Del: integrated -- https://master.php.net/note/delete/95952/integrated
Del: useless -- https://master.php.net/note/delete/95952/useless
Del: bad code -- https://master.php.net/note/delete/95952/bad+code
Del: spam -- https://master.php.net/note/delete/95952/spam
Del: non-english -- https://master.php.net/note/delete/95952/non-english
Del: in docs -- https://master.php.net/note/delete/95952/in+docs
Del: other reasons-- https://master.php.net/note/delete/95952
Reject -- https://master.php.net/note/reject/95952
Search -- https://master.php.net/manage/user-notes.php