Chapter 9. Expressions
PHP takes expressions much further, in the same way many other
languages do. PHP is an expression-oriented language, in the
sense that almost everything is an expression. Consider the
example we've already dealt with, '$a = 5'. It's easy to see
that there are two values involved here, the value of the
integer constant '5', and the value of $a which is being updated
to 5 as well. But the truth is that there's one additional
value involved here, and that's the value of the assignment
itself. The assignment itself evaluates to the assigned value,
in this case 5. In practice, it means that '$a = 5', regardless
of what it does, is an expression with the value 5. Thus,
writing something like '$b = ($a = 5)' is like writing '$a
= 5; $b = 5;' (a semicolon marks the end of a statement).
Since assignments are parsed in a right to left order, you
can also write '$b = $a = 5'.
Another good example of expression orientation is pre-
and post-increment and decrement. Users of PHP and many
other languages may be familiar with the notation of variable++
and variable--. These are increment and decrement operators.
In PHP, the statement '$a++' has no value (is not an expression),
and thus you can't assign it or use it in any way. PHP enhances
the increment/decrement capabilities by making these expressions
as well, like in C. In PHP, like in C, there are two types
of increment - pre-increment and post-increment. Both pre-increment
and post-increment essentially increment the variable, and
the effect on the variable is identical. The difference
is with the value of the increment expression. Pre-increment,
which is written '++$variable', evaluates to the incremented
value (PHP increments the variable before reading its value,
thus the name 'pre-increment'). Post-increment, which is
written '$variable++' evaluates to the original value of
$variable, before it was incremented (PHP increments the
variable after reading its value, thus the name 'post-increment').
A very common type of expressions are comparison expressions.
These expressions evaluate to either FALSE or TRUE. PHP
supports > (bigger than), >= (bigger than or equal
to), == (equal), != (not equal), < (smaller than) and
<= (smaller than or equal to). The language also supports
a set of strict equivalence operators: === (equal to and
same type) and !== (not equal to or not same type). These
expressions are most commonly used inside conditional execution,
such as if statements.
The last example of expressions we'll deal with here is
combined operator-assignment expressions. You already know
that if you want to increment $a by 1, you can simply write
'$a++' or '++$a'. But what if you want to add more than
one to it, for instance 3? You could write '$a++' multiple
times, but this is obviously not a very efficient or comfortable
way. A much more common practice is to write '$a = $a +
3'. '$a + 3' evaluates to the value of $a plus 3, and is
assigned back into $a, which results in incrementing $a
by 3. In PHP, as in several other languages like C, you
can write this in a shorter way, which with time would become
clearer and quicker to understand as well. Adding 3 to the
current value of $a can be written '$a += 3'. This means
exactly "take the value of $a, add 3 to it, and assign
it back into $a". In addition to being shorter and
clearer, this also results in faster execution. The value
of '$a += 3', like the value of a regular assignment, is
the assigned value. Notice that it is NOT 3, but the combined
value of $a plus 3 (this is the value that's assigned into
$a). Any two-place operator can be used in this operator-assignment
mode, for example '$a -= 5' (subtract 5 from the value of
$a), '$b *= 7' (multiply the value of $b by 7), etc.