PHP 7.2.0 Release Candidate 2 Released

Voting

Please answer this simple SPAM challenge: min(three, three)?
(Example: nine)

The Note You're Voting On

anthony at n dot o dot s dot p dot a dot m dot trams dot com
16 years ago
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' ) ;

<< Back to user notes page

To Top