note 95952 modified in language.operators.comparison by danbrown
| From: | danbrown@php.net | Date: | Sat, 30 Jan 2010 19:08:44 +0000 |
| Subject: | note 95952 modified in language.operators.comparison by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165518@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...
<?php
$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...
<?php
$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.)
--was--
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.)
http://php.net/manual/en/language.operators.comparison.php