## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

## 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. It’s familiar to anyone who has taken elementary-school algebra:

`y = 3(abs(2x) + 4)`

which in PHP would be:

`\$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—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 of the expressions (the `<br />` tag is there to create a line break and thus separate the output into four lines in HTML).

Example 4-1. Four simple Boolean expressions
```<?php
echo "a: [" . (20 > 9) . "]<br />";
echo "b: [" . (5 == 6) . "]<br />";
echo "c: [" . (1 == 0) . "]<br />";
echo "d: [" . (1 == 1) . "]<br />";
?>```

The output from this code is as follows:

```a: [1]
b: []
c: []
d: [1]```

Notice that both expressions `a:` and `d:` evaluate to `TRUE`, which has a value of 1. But `b:` and `c:`, which evaluate to `FALSE`, do not show any value, because in PHP the constant `FALSE` is defined as `NULL`, or nothing. To verify this for yourself, you could enter the code in Example 4-2.

Example 4-2. Outputting the values of TRUE and FALSE
```<?php // test2.php
echo "a: [" . TRUE  . "]<br />";
echo "b: [" . FALSE . "]<br />";
?>```

which outputs the following:

```a: [1]
b: []```

By the way, in some languages `FALSE` may be defined as 0 or even −1, so it’s worth checking on its definition in each language.

### Literals and Variables

The simplest form of an expression is a literal, which simply means something that evaluates to itself, such as the number 73 or the string “Hello”. An expression could also simply be a variable, which evaluates to the value that has been assigned to it. They are both types of expressions, because they return a value.

Example 4-3 shows five different literals, all of which return values, albeit of different types.

Example 4-3. Five types of literals
```<?php
\$myname = "Brian";
\$myage = 37;
echo "a: " . 73      . "<br />"; // Numeric literal
echo "b: " . "Hello" . "<br />"; // String literal
echo "c: " . FALSE   . "<br />"; // Constant literal
echo "d: " . \$myname . "<br />"; // Variable string literal
echo "e: " . \$myage  . "<br />"; // Variable numeric literal
?>```

And, as you’d expect, you see a return value from all of these with the exception of `c:`, which evaluates to `FALSE`, returning nothing in the following output:

```a: 73
b: Hello
c:
d: Brian
e: 37```

In conjunction with operators, it’s possible to create more complex expressions that evaluate to useful results.

When you combine assignment or control-flow constructs with expressions, the result is a statement. Example 4-4 shows one of each. The first assigns the result of the expression `366 - \$day_number` to the variable `\$days_to_new_year`, and the second outputs a friendly message only if the expression `\$days_to_new_year < 30` evaluates to `TRUE`.

Example 4-4. An expression and a statement
```<?php
\$days_to_new_year = 366 - \$day_number;  // Expression
if (\$days_to_new_year < 30)
{
echo "Not long now till new year"; // Statement
}
?>```

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required