note 100208 deleted from control-structures.alternative-syntax by danbrown
| From: | danbrown@php.net | Date: | Fri, 01 Oct 2010 12:01:26 +0000 |
| Subject: | note 100208 deleted from control-structures.alternative-syntax by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172874@lists.php.net to get a copy of this message | ||
Note Submitter: A dot Lepe dev+php at alepe dot com
----
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.