PHP Ternary Operator – The "IF-Else" Shortcut

By | 2010-06-22

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;

Leave a Reply

Your email address will not be published. Required fields are marked *