note 9801 modified in Expressions by ronabop
| From: | ronabop@php.net | Date: | Wed, 06 Dec 2000 05:53:34 +0000 |
| Subject: | note 9801 modified in Expressions by ronabop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-8444@lists.php.net to get a copy of this message | ||
The ternary conditional operator is a useful way of avoiding inconvenient if statements. They can
even be used in the middle of a string concatenation, if you use parentheses.
Example:
if ( $wakka ) {
$string = 'foo' ;
} else {
$string = 'bar' ;
}
The above can be expressed like the following:
$string = $wakka ? 'foo' : 'bar' ;
If $wakka is true, $string is assigned 'foo', and if it's false, $string is assigned
'bar'.
To do the same in a concatenation, try:
$string = $otherString . ( $wakka ? 'foo' : 'bar' ) ;
http://www.php.net/manual/expressions.php