note 79019 deleted from control-structures.alternative-syntax by nlopess
| From: | nlopess@php.net | Date: | Thu, 08 Nov 2007 23:43:35 +0000 |
| Subject: | note 79019 deleted from control-structures.alternative-syntax by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-132135@lists.php.net to get a copy of this message | ||
Note Submitter: SID TRIVEDI
----
<?php
/*
TENARY OPERATOR : ALTERNATIVE SYNTAX
Expression - 1 (True or False) ? Expression - 2 (True) : Expression -3 (False);
Condition to be evaluated or Boolean Expression to be evaluated True or False ? Codes to be excuted
, if true : codes to be executed, if false
The following codes can be replaced by following tenary expression as shown:
*/
$a=24;
/* // Codes replacement by tenary expression.
if($a == 1)
{
$var = "Value is One";
}
else if($a == 24)
{
$var = "24th Avenue";
}
else
{
$var = "Not equal to One nor 24";
}
*/
$var = $a == 1 ? "Value is One" : ($a == 24 ? "24th Avenue" : "Not equal to
One nor 24");
echo "$var\n<br>";
/*
Which generates following output as $a = 24
24th Avenue
More information on tenary expression can be found on following links:
http://www.phpvalue.com/what-is-php-ternary-opeartor/
http://www.rasyid.net/2006/11/20/php-ternary-operator/
*/
?>