true, such as a nonzero number), the conditional statement evaluates to
true.
Special Operators
The ternary operator and the execution operator work differently from the
operators you 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 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:
Click here to view code image
$age_description = ($age < 18) ? "child" : "adult";
Without explanation, this code is essentially meaningless; however, it expands
into the following five lines of code:
Click here to view code image
if ($age < 18) {
$age_description = "child";
} else {
$age_description = "adult";
}
The ternary operator is so named because it has three operands: a condition to
check ($age < 18 in the previous code), a result if the condition is true
(“child”), and a result if the condition is false (“adult”). Although
we hope you never have to use the ternary operator, it is at least important to
know how it works in case you stumble across it.
The other special operator is the execution operator, which is the backtick
symbol (`). The position of the backtick key varies depending on your
keyboard, but it is likely to be just to the left of the 1 key (above Tab). The
execution operator executes the program inside the backticks, returning any
text the program outputs. Here is an example:
Click here to view code image
<?php
$i = ls –l
;
echo $i;
?>
This executes the ls program, passing in -l (a lowercase L) to get the long