Short Ternary Operator

May 18 2011

With the release of PHP 5.3.0 there is an option to shorten the ternary operator.

So, if we start with variable $foo, which is true ($foo = true), instead of writing:

$bar = $foo ? true : false; // $bar = true

you can easily shorten it like this

$bar = $foo ?: false // $bar = true

The bad side of using a short ternary operator is that most of PHP IDEs are marking it as syntax error (well, at least for now)

Comments Off

Comments are closed at this time.

Performance Optimization WordPress Plugins by W3 EDGE