note 98052 added to language.operators.comparison
| From: | michal dot kocarek at brainbox dot cz | Date: | Sun, 23 May 2010 18:05:28 +0000 |
| Subject: | note 98052 added to language.operators.comparison | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169533@lists.php.net to get a copy of this message | ||
I am posting another hint about formatting of conditional expression blocks. It does take more
lines, but lines starting with "?" and ":" allow to distinguish ternary operator
quite quickly.
<?php
// Single ternary operator
$sentence = $value == 1
? 'You have one egg.'
: 'You have '.$value.' eggs.';
// Nested ternary operators
$sentence = $value == 1
? 'You have one egg.'
: ($value == 0
? 'You have no eggs.'
($value == 2
? 'You have two eggs.'
: 'You have '.$value.' eggs.'
);
?>
PS: In case on wondering about concatenation "." operator inside the expression:
concatenation has higher priority than ternary operator, so it is evaluated first.
----
Server IP: 69.147.83.197
Probable Submitter: 89.24.95.254
----
Manual Page -- http://www.php.net/manual/en/language.operators.comparison.php
Edit -- https://master.php.net/note/edit/98052
Del: integrated -- https://master.php.net/note/delete/98052/integrated
Del: useless -- https://master.php.net/note/delete/98052/useless
Del: bad code -- https://master.php.net/note/delete/98052/bad+code
Del: spam -- https://master.php.net/note/delete/98052/spam
Del: non-english -- https://master.php.net/note/delete/98052/non-english
Del: in docs -- https://master.php.net/note/delete/98052/in+docs
Del: other reasons-- https://master.php.net/note/delete/98052
Reject -- https://master.php.net/note/reject/98052
Search -- https://master.php.net/manage/user-notes.php