note 9801 added to Expressions
| From: | anthony at n dot o dot s dot p dot a dot m dot trams dot com | Date: | Fri, 24 Nov 2000 16:01:52 +0000 |
| Subject: | note 9801 added to Expressions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-8105@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/language.expressions.php