note 97341 added to control-structures.alternative-syntax
| From: | mulllhausen at osu1 dot php dot net | Date: | Thu, 15 Apr 2010 08:08:48 +0000 |
| Subject: | note 97341 added to control-structures.alternative-syntax | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168258@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 69.147.83.197
Probable Submitter: 150.101.100.168
----
Manual Page -- http://www.php.net/manual/en/control-structures.alternative-syntax.php
Edit -- https://master.php.net/note/edit/97341
Del: integrated -- https://master.php.net/note/delete/97341/integrated
Del: useless -- https://master.php.net/note/delete/97341/useless
Del: bad code -- https://master.php.net/note/delete/97341/bad+code
Del: spam -- https://master.php.net/note/delete/97341/spam
Del: non-english -- https://master.php.net/note/delete/97341/non-english
Del: in docs -- https://master.php.net/note/delete/97341/in+docs
Del: other reasons-- https://master.php.net/note/delete/97341
Reject -- https://master.php.net/note/reject/97341
Search -- https://master.php.net/manage/user-notes.php