note 100208 added to control-structures.alternative-syntax
| From: | A dot Lepedev+php at alepe dot com | Date: | Fri, 01 Oct 2010 06:20:49 +0000 |
| Subject: | note 100208 added to control-structures.alternative-syntax | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172863@lists.php.net to get a copy of this message | ||
You can perform if/elseif/else in one line like this:
(Parentheses are not required but it looks better)
<?php
$c = "c";
echo ($c == "a") ? 1 : ($c == "b") ? 2 : ($c == "c") ? 3 : 0;
//result: 3
?>
The previous code is equivalent of:
<?php
$c = "c";
if($c == "a")
echo 1;
elseif ($c == "b")
echo 2;
elseif ($c == "c")
echo 3;
else
echo 0;
?>
I couldn't find any documentation about it, so I'm posting it here.
----
Server IP: 69.147.83.197
Probable Submitter: 211.6.179.194
----
Manual Page -- http://www.php.net/manual/en/control-structures.alternative-syntax.php
Edit -- https://master.php.net/note/edit/100208
Del: integrated -- https://master.php.net/note/delete/100208/integrated
Del: useless -- https://master.php.net/note/delete/100208/useless
Del: bad code -- https://master.php.net/note/delete/100208/bad+code
Del: spam -- https://master.php.net/note/delete/100208/spam
Del: non-english -- https://master.php.net/note/delete/100208/non-english
Del: in docs -- https://master.php.net/note/delete/100208/in+docs
Del: other reasons-- https://master.php.net/note/delete/100208
Reject -- https://master.php.net/note/reject/100208
Search -- https://master.php.net/manage/user-notes.php