<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Expressions</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.constants.predefined.html">Magic constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.operators.html">Operators</a></div> <div class="up"><a href="langref.html">Language Reference</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.expressions" class="chapter"> <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"><var class="varname">$a</var></var> = 5", you're assigning '5' into <var class="varname"><var class="varname">$a</var></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"><var class="varname">$a</var></var>'s value to be 5 as well, so if you wrote <var class="varname"><var class="varname">$b</var></var> = <var class="varname"><var class="varname">$a</var></var>, you'd expect it to behave just as if you wrote <var class="varname"><var class="varname">$b</var></var> = 5. In other words, <var class="varname"><var class="varname">$a</var></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: <div class="informalexample"> <div class="example-contents"> <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 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.html" class="link">functions</a>), you'd assume that typing <em>$c = foo()</em> is essentially just like writing <em>$c = 5</em>, and you're right. Functions are expressions with the value of their return value. Since <em>foo()</em> returns 5, the value of the expression '<em>foo()</em>' 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: <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> values, floating point values (<span class="type"><a href="language.types.float.html" class="type float">float</a></span>), <span class="type"><a href="language.types.string.html" class="type string">string</a></span> values and <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> 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"><var class="varname">$a</var></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"><var class="varname">$a</var></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"><var class="varname">$a</var></var> = 5', regardless of what it does, is an expression with the value 5. Thus, writing something like '<var class="varname"><var class="varname">$b</var></var> = (<var class="varname"><var class="varname">$a</var></var> = 5)' is like writing '<var class="varname"><var class="varname">$a</var></var> = 5; <var class="varname"><var class="varname">$b</var></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"><var class="varname">$b</var></var> = <var class="varname"><var class="varname">$a</var></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 <em>variable++</em> and <em>variable--</em>. These are <a href="language.operators.increment.html" class="link"> increment and decrement operators</a>. 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"><var class="varname">$variable</var></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"><var class="varname">$variable</var></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.html" class="link">comparison</a> expressions. These expressions evaluate to either <strong><code>FALSE</code></strong> or <strong><code>TRUE</code></strong>. 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 <em>if</em> 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"><var class="varname">$a</var></var> by 1, you can simply write '<var class="varname"><var class="varname">$a</var></var>++' or '++<var class="varname"><var class="varname">$a</var></var>'. But what if you want to add more than one to it, for instance 3? You could write '<var class="varname"><var class="varname">$a</var></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"><var class="varname">$a</var></var> = <var class="varname"><var class="varname">$a</var></var> + 3'. '<var class="varname"><var class="varname">$a</var></var> + 3' evaluates to the value of <var class="varname"><var class="varname">$a</var></var> plus 3, and is assigned back into <var class="varname"><var class="varname">$a</var></var>, which results in incrementing <var class="varname"><var class="varname">$a</var></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"><var class="varname">$a</var></var> can be written '<var class="varname"><var class="varname">$a</var></var> += 3'. This means exactly "take the value of <var class="varname"><var class="varname">$a</var></var>, add 3 to it, and assign it back into <var class="varname"><var class="varname">$a</var></var>". In addition to being shorter and clearer, this also results in faster execution. The value of '<var class="varname"><var class="varname">$a</var></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"><var class="varname">$a</var></var> plus 3 (this is the value that's assigned into <var class="varname"><var class="varname">$a</var></var>). Any two-place operator can be used in this operator-assignment mode, for example '<var class="varname"><var class="varname">$a</var></var> -= 5' (subtract 5 from the value of <var class="varname"><var class="varname">$a</var></var>), '<var class="varname"><var class="varname">$b</var></var> *= 7' (multiply the value of <var class="varname"><var class="varname">$b</var></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"> <div class="informalexample"> <div class="example-contents"> <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 class="para"> If the value of the first subexpression is <strong><code>TRUE</code></strong> (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"> <div class="informalexample"> <div class="example-contents"> <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 class="simpara"> Some expressions can be considered as statements. In this case, a statement has the form of '<em>expr ;</em>' that is, an expression followed by a semicolon. In <em>'$b = $a = 5;'</em>, <em>'$a = 5'</em> is a valid expression, but it's not a statement by itself. <em>'$b = $a = 5;'</em> 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 <strong><code>TRUE</code></strong> or <strong><code>FALSE</code></strong>. The constants <strong><code>TRUE</code></strong> and <strong><code>FALSE</code></strong> (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.html#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"><var class="varname">expr</var></var> to indicate any valid PHP expression. </p> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.constants.predefined.html">Magic constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.operators.html">Operators</a></div> <div class="up"><a href="langref.html">Language Reference</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>