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 it has three operands: a condition ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access