Chapter 9. Expressions
There is one more expression that may seem odd if you haven't
seen it in other languages, the ternary conditional operator:
<?php
$first ? $second : $third
?>
If the value of the first subexpression is TRUE (non-zero),
then the second subexpression is evaluated, and that is
the result of the conditional expression. Otherwise, the
third subexpression is evaluated, and that is the value.
The following example should help you understand pre- and
post-increment and expressions in general a bit better:
<?php
function double($i)
{
return $i*2;
}
$b = $a = 5; /* assign the value five into the variable
$a and $b */
$c = $a++; /* post-increment, assign original value of $a
(5) to $c */
$e = $d = ++$b; /* pre-increment, assign the incremented
value of
$b (6) to $d and $e */
/* at this point, both $d and $e are equal to 6 */
$f = double($d++); /* assign twice the value of $d before
the increment, 2*6 = 12 to $f */
$g = double(++$e); /* assign twice the value of $e after
the increment, 2*7 = 14 to $g */
$h = $g += 10; /* first, $g is incremented by 10 and ends
with the
value of 24. the value of the assignment (24) is
then assigned into $h, and $h ends with the value
of 24 as well. */
?>
Some expressions can be considered as statements. In this
case, a statement has the form of 'expr' ';' that is, an
expression followed by a semicolon. In '$b=$a=5;', $a=5
is a valid expression, but it's not a statement by itself.
'$b=$a=5;' however is a valid statement.
One last thing worth mentioning is the truth value of expressions.
In many events, mainly in conditional execution and loops,
you're not interested in the specific value of the expression,
but only care about whether it means TRUE or FALSE. The
constants TRUE and FALSE (case-insensitive) are the two
possible boolean values. When necessary, an expression is
automatically converted to boolean. See the section about
type-casting for details about how.
PHP provides a full and powerful implementation of expressions,
and documenting it entirely goes beyond the scope of this
manual. The above examples should give you a good idea about
what expressions are and how you can construct useful expressions.
Throughout the rest of this manual we'll write expr to indicate
any valid PHP expression.