Expressions
Let’s start with the most fundamental part of any programming language: expressions.
An expression is a combination of values, variables, operators, and functions that results in a value. Anyone who has taken an algebra class should recognize this sort of expression:
y = 3(abs(2x) + 4)
which in PHP would be written as:
$y = 3 * (abs(2*$x) + 4);
The value returned (y or $y in this case) can be a number, a string, or a
Boolean value (named after George Boole, a
nineteenth-century English mathematician and philosopher). By now, you
should be familiar with the first two value types, but I’ll explain the
third.
A basic Boolean value can be either TRUE or FALSE. For example, the expression 20 > 9 (20 is greater than 9) is TRUE, and the expression 5 == 6 (5 is equal to 6) is FALSE. (Boolean operations can be combined using
operators such as AND, OR, and XOR,
which are covered later in this chapter.)
Note that I am using uppercase letters for the names TRUE and FALSE. This is because they are predefined
constants in PHP. You can also use the lowercase versions, if you prefer,
as they are also predefined. In fact, the lowercase versions are more
stable, because PHP does not allow you to redefine them; the uppercase
ones may be redefined, which is something you should bear in mind if you
import third-party code.
Example 4-1 shows some simple
expressions: the two I just mentioned, plus a couple more. For each line,
it prints out a letter between a and
d, followed by a colon and the result ...
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