note 84184 modified in language.expressions by googleguy

From: Date: Thu, 20 Sep 2012 00:35:37 +0000
Subject: note 84184 modified in language.expressions by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190894@lists.php.net to get a copy of this message
The ternary operator is a convenient way to conditionally return an expression. 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' ) ; --was-- The ternary conditional operator is a useful way of avoiding http://phpsourcecode.blogspot.com 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://php.net/manual/en/language.expressions.php

« previous php.notes (#190894) next »