PHP / Programming · 2010-06-22

PHP Ternary Operator – The "IF-Else" Shortcut

A ternary operator is a quick shortcut to bypass the multi-line if-else statement. It is sufficient when simple setting a value of a variable based on a condition.

It looks like this:

//
    $variable == 10 ? $x = true : $x = false; 
//

That is saying “if $variable equals 10 then set $x equal to true, otherwise set $x equal to false”.

In a nutshell, condition? true : false;