Special Operators

The ternary operator and the execution operator work differently from those we have seen so far. The ternary operator is rarely used in PHP, thankfully, because it is really just a condensed conditional statement. Presumably it arose through someone needing to make a code occupy as little space as possible because it certainly does not make PHP code any easier to read.

The ternary operator works like this:

$age_description = ($age < 18) ? "child" : "adult";

Without explanation, that code is essentially meaningless; however, it expands into the following five lines of code:

if ($age < 18) {  $age_description = "child";} else {  $age_description = "adult";}

The ternary operator is so named because ...

Get Ubuntu Unleashed 2014 Edition: Covering 13.10 and 14.04,Ninth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.