Source of: /manual/en/language.expressions.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/langref.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'language.expressions.php',
1 => 'Expressions',
),
'up' =>
array (
0 => 'langref.php',
1 => 'Language Reference',
),
'prev' =>
array (
0 => 'language.constants.predefined.php',
1 => 'Magic constants',
),
'next' =>
array (
0 => 'language.operators.php',
1 => 'Operators',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div>
<h1>Expressions</h1>
<p class="simpara">
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".
</p>
<p class="simpara">
The most basic forms of expressions are constants and variables.
When you type "<var class="varname">$a</var> = 5", you're assigning '5' into
<var class="varname">$a</var>. '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).
</p>
<p class="simpara">
After this assignment, you'd expect <var class="varname">$a</var>'s value to be 5 as
well, so if you wrote <var class="varname">$b</var> = <var class="varname">$a</var>, you'd expect it to behave just as
if you wrote <var class="varname">$b</var> = 5. In other words, <var class="varname">$a</var> is an expression with the
value of 5 as well. If everything works right, this is exactly
what will happen.
</p>
<p class="para">
Slightly more complex examples for expressions are functions. For
instance, consider the following function:
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo </span><span style="color: #007700">()<br />{<br /> return </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="simpara">
Assuming you're familiar with the concept of functions (if you're
not, take a look at the chapter about <a href="language.functions.php" class="link">functions</a>), you'd assume
that typing <i>$c = foo()</i> is essentially just like
writing <i>$c = 5</i>, and you're right. Functions
are expressions with the value of their return value. Since <i>foo()</i>
returns 5, the value of the expression '<i>foo()</i>' is 5. Usually
functions don't just return a static value but compute something.
</p>
<p class="simpara">
Of course, values in PHP don't have to be integers, and very often
they aren't. PHP supports four scalar value types: <a href="language.types.integer.php" class="type integer">integer</a>
values, floating point values (<a href="language.types.float.php" class="type float">float</a>), <a href="language.types.string.php" class="type string">string</a>
values and <a href="language.types.boolean.php" class="type boolean">boolean</a> 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.
</p>
<p class="simpara">
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, '<var class="varname">$a</var> = 5'. It's easy to see that
there are two values involved here, the value of the integer
constant '5', and the value of <var class="varname">$a</var> 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 '<var class="varname">$a</var> = 5', regardless of what it does,
is an expression with the value 5. Thus, writing something like
'<var class="varname">$b</var> = (<var class="varname">$a</var> = 5)' is like writing
'<var class="varname">$a</var> = 5; <var class="varname">$b</var> = 5;' (a semicolon
marks the end of a statement). Since assignments are parsed in a
right to left order, you can also write '<var class="varname">$b</var> = <var class="varname">$a</var> = 5'.
</p>
<p class="simpara">
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 <i>variable++</i> and
<i>variable--</i>. These are <a href="language.operators.increment.php" class="link">
increment and decrement operators</a>. In
PHP/FI 2, the statement '<var class="varname">$a</var>++' 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 '++<var class="varname">$variable</var>', evaluates to the
incremented value (PHP increments the variable before reading its
value, thus the name 'pre-increment'). Post-increment, which is
written '<var class="varname">$variable</var>++' evaluates to the original value of
$variable, before it was incremented (PHP increments the variable
after reading its value, thus the name 'post-increment').
</p>
<p class="simpara">
A very common type of expressions are <a href="language.operators.comparison.php" class="link">comparison</a>
expressions. These expressions evaluate to either <b><tt class="constant">FALSE</tt></b> or <b><tt class="constant">TRUE</tt></b>. 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 <i>if</i> statements.
</p>
<p class="simpara">
The last example of expressions we'll deal with here is combined
operator-assignment expressions. You already know that if you
want to increment <var class="varname">$a</var> by 1, you can simply write
'<var class="varname">$a</var>++' or '++<var class="varname">$a</var>'.
But what if you want to add more than one to it, for instance 3?
You could write '<var class="varname">$a</var>++' multiple times, but this
is obviously not a very efficient or comfortable way. A much more
common practice is to write '<var class="varname">$a</var> =
<var class="varname">$a</var> + 3'. '<var class="varname">$a</var> + 3' evaluates
to the value of <var class="varname">$a</var> plus 3, and is assigned back
into <var class="varname">$a</var>, which results in incrementing <var class="varname">$a</var>
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 <var class="varname">$a</var>
can be written '<var class="varname">$a</var> += 3'. This means exactly
"take the value of <var class="varname">$a</var>, add 3 to it, and assign it
back into <var class="varname">$a</var>". In addition to being shorter and
clearer, this also results in faster execution. The value of
'<var class="varname">$a</var> += 3', like the value of a regular assignment, is
the assigned value. Notice that it is NOT 3, but the combined value
of <var class="varname">$a</var> plus 3 (this is the value that's
assigned into <var class="varname">$a</var>). Any two-place operator can be used
in this operator-assignment mode, for example '<var class="varname">$a</var> -= 5'
(subtract 5 from the value of <var class="varname">$a</var>), '<var class="varname">$b</var> *= 7'
(multiply the value of <var class="varname">$b</var> by 7), etc.
</p>
<p class="para">
There is one more expression that may seem odd if you haven't seen
it in other languages, the ternary conditional operator:
</p>
<p class="para">
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$first </span><span style="color: #007700">? </span><span style="color: #0000BB">$second </span><span style="color: #007700">: </span><span style="color: #0000BB">$third<br />?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
If the value of the first subexpression is <b><tt class="constant">TRUE</tt></b> (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.
</p>
<p class="para">
The following example should help you understand pre- and
post-increment and expressions in general a bit better:
</p>
<p class="para">
</p><div class="informalexample">
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">double</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">)<br />{<br /> return </span><span style="color: #0000BB">$i</span><span style="color: #007700">*</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">; </span><span style="color: #FF8000">/* assign the value five into the variable $a and $b */<br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">$a</span><span style="color: #007700">++; </span><span style="color: #FF8000">/* post-increment, assign original value of $a <br /> (5) to $c */<br /></span><span style="color: #0000BB">$e </span><span style="color: #007700">= </span><span style="color: #0000BB">$d </span><span style="color: #007700">= ++</span><span style="color: #0000BB">$b</span><span style="color: #007700">; </span><span style="color: #FF8000">/* pre-increment, assign the incremented value of <br /> $b (6) to $d and $e */<br /><br />/* at this point, both $d and $e are equal to 6 */<br /><br /></span><span style="color: #0000BB">$f </span><span style="color: #007700">= </span><span style="color: #0000BB">double</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">++); </span><span style="color: #FF8000">/* assign twice the value of $d before<br /> the increment, 2*6 = 12 to $f */<br /></span><span style="color: #0000BB">$g </span><span style="color: #007700">= </span><span style="color: #0000BB">double</span><span style="color: #007700">(++</span><span style="color: #0000BB">$e</span><span style="color: #007700">); </span><span style="color: #FF8000">/* assign twice the value of $e after<br /> the increment, 2*7 = 14 to $g */<br /></span><span style="color: #0000BB">$h </span><span style="color: #007700">= </span><span style="color: #0000BB">$g </span><span style="color: #007700">+= </span><span style="color: #0000BB">10</span><span style="color: #007700">; </span><span style="color: #FF8000">/* first, $g is incremented by 10 and ends with the <br /> value of 24. the value of the assignment (24) is <br /> then assigned into $h, and $h ends with the value <br /> of 24 as well. */<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="simpara">
Some expressions can be considered as statements. In
this case, a statement has the form of '<i>expr ;</i>' that is, an
expression followed by a semicolon. In <i>'$b = $a = 5;'</i>,
<i>'$a = 5'</i> is a valid expression, but it's not a statement
by itself. <i>'$b = $a = 5;'</i> however is a valid statement.
</p>
<p class="simpara">
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 <b><tt class="constant">TRUE</tt></b> or <b><tt class="constant">FALSE</tt></b>.
The constants <b><tt class="constant">TRUE</tt></b> and <b><tt class="constant">FALSE</tt></b> (case-insensitive) are the two
possible boolean values. When necessary, an expression is
automatically converted to boolean. See the
<a href="language.types.type-juggling.php#language.types.typecasting" class="link">section about
type-casting</a> for details about how.
</p>
<p class="simpara">
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 <var class="varname">expr</var>
to indicate any valid PHP expression.
</p>
</div>
<?php manual_footer(); ?>