Chapter 9. Expressions
Expressions are the most important building stones of PHP.
In PHP, almost anything you write is an expression. The simplest
yet most accurate way to define an expression is "anything
that has a value".
The most basic forms of expressions are constants and variables.
When you type "$a = 5", you're assigning '5' into
$a. '5', obviously, has the value 5, or in other words '5'
is an expression with the value of 5 (in this case, '5'
is an integer constant).
After this assignment, you'd expect $a's value to be 5
as well, so if you wrote $b = $a, you'd expect it to behave
just as if you wrote $b = 5. In other words, $a is an expression
with the value of 5 as well. If everything works right,
this is exactly what will happen.
Slightly more complex examples for expressions are functions.
For instance, consider the following function:
<?php
function foo ()
{
return 5;
}
?>
Assuming you're familiar with the concept of functions
(if you're not, take a look at the chapter about functions),
you'd assume that typing $c = foo() is essentially just
like writing $c = 5, and you're right. Functions are expressions
with the value of their return value. Since foo() returns
5, the value of the expression 'foo()' is 5. Usually functions
don't just return a static value but compute something.
Of course, values in PHP don't have to be integers, and
very often they aren't. PHP supports four scalar value types:
integer values, floating point values (float), string values
and boolean values (scalar values are values that you can't
'break' into smaller pieces, unlike arrays, for instance).
PHP also supports two composite (non-scalar) types: arrays
and objects. Each of these value types can be assigned into
variables or returned from functions.