note 97341 deleted from control-structures.alternative-syntax by danbrown
| From: | danbrown@php.net | Date: | Thu, 15 Apr 2010 12:55:04 +0000 |
| Subject: | note 97341 deleted from control-structures.alternative-syntax by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168273@lists.php.net to get a copy of this message | ||
Note Submitter: mulllhausen
----
the following may be helpful to someone:
<?php
//the alternative syntax for 'if' must always evaluate to a single value:
$res = (true ? 'yes' : 'no');
echo $res;
//note that it is not necessary to assign the value to a variable, you may do whatever you would
normally do with a value:
echo (true ? 'yes' : 'no');
//or:
if((true ? 88 : 99) > 90) echo 'number is above 90';
else echo 'number is below 90';
//note also that the brackets are not necessary:
$res = true ? 'yes' : 'no';
echo $res;
//furthermore it is not neccessary to do anything with the resulting value:
true ? 'yes' : 'no';
//this seems a bit useless until you consider that it can also be used to assign values to
variables:
true ? $var = 'yes' : $var = 'no';
echo "var is $var";
//which only works because it is equivalent to
$res = true ? $var = 'yes' : $var = 'no';
//ie
$res = $var = 'yes';
//which is fine. note however that
true ? echo 'yes' : echo 'no';
//causes syntax errors since it is effectively trying to do:
$res = echo 'yes';
//which is obviously not allowed
?>