<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'language.expressions.php',
    1 => 'Expressions',
    2 => 'Expressions',
  ),
  'up' => 
  array (
    0 => 'langref.php',
    1 => 'Довідник з мови',
  ),
  'prev' => 
  array (
    0 => 'language.constants.magic.php',
    1 => 'Magic constants',
  ),
  'next' => 
  array (
    0 => 'language.operators.php',
    1 => 'Operators',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/expressions.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.expressions" class="chapter">
   <h1 class="title">Expressions</h1>

   <p class="simpara">
    Expressions are the most important building blocks of PHP. In PHP,
    almost anything you write is an expression. The simplest yet
    most accurate way to define an expression is &quot;anything that has a
    value&quot;.
   </p>
   <p class="simpara">
    The most basic forms of expressions are constants and variables.
    When you type <code class="code">$a = 5</code>, you&#039;re assigning <code class="code">5</code> into
    <var class="varname">$a</var>. <code class="code">5</code>, obviously,
    has the value 5, or in other words <code class="code">5</code> is an expression with the
    value of 5 (in this case, <code class="code">5</code> is an integer constant).
   </p>
   <p class="simpara">
    After this assignment, you&#039;d expect <var class="varname">$a</var>&#039;s value to be 5 as
    well, so if you wrote <code class="code">$b = $a</code>, you&#039;d expect it to behave just as
    if you wrote <code class="code">$b = 5</code>. 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:
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
   <p class="simpara">
    Assuming you&#039;re familiar with the concept of functions (if you&#039;re
    not, take a look at the chapter about <a href="language.functions.php" class="link">functions</a>), you&#039;d assume
    that typing <code class="code">$c = foo()</code> is essentially just like
    writing <code class="code">$c = 5</code>, and you&#039;re right. Functions
    are expressions with the value of their return value. Since <code class="code">foo()</code>
    returns 5, the value of the expression &#039;<code class="code">foo()</code>&#039; is 5. Usually
    functions don&#039;t just return a static value but compute something.
   </p>
   <p class="simpara">
    Of course, values in PHP don&#039;t have to be integers, and very often
    they aren&#039;t. PHP supports four scalar value types: <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>
    values, floating point values (<span class="type"><a href="language.types.float.php" class="type float">float</a></span>), <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
    values and <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> values (scalar values are values that you
    can&#039;t &#039;break&#039; 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&#039;ve already dealt with, <code class="code">$a = 5</code>. It&#039;s easy to see that
    there are two values involved here, the value of the integer
    constant <code class="code">5</code>, and the value of <var class="varname">$a</var> which is being updated to 5 as
    well. But the truth is that there&#039;s one additional value involved
    here, and that&#039;s the value of the assignment itself. The
    assignment itself evaluates to the assigned value, in this case 5.
    In practice, it means that <code class="code">$a = 5</code>, regardless of what it does,
    is an expression with the value 5. Thus, writing something like
    <code class="code">$b = ($a = 5)</code> is like writing
    <code class="code">$a = 5; $b = 5;</code> (a semicolon
    marks the end of a statement). Since assignments are parsed in a
    right to left order, you can also write <code class="code">$b = $a = 5</code>.
   </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 <code class="code">variable++</code> and
    <code class="code">variable--</code>. These are <a href="language.operators.increment.php" 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 <code class="code">++$variable</code>, evaluates to the
    incremented value (PHP increments the variable before reading its
    value, thus the name &#039;pre-increment&#039;). Post-increment, which is
    written <code class="code">$variable++</code> evaluates to the original value of
    <var class="varname">$variable</var>, before it was incremented (PHP increments the variable
    after reading its value, thus the name &#039;post-increment&#039;).
   </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  <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> or <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>. PHP
    supports &gt; (bigger than), &gt;= (bigger than or equal to), == (equal),
    != (not equal), &lt; (smaller than) and &lt;= (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 <code class="code">if</code> statements.
   </p>
   <p class="simpara">
    The last example of expressions we&#039;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
    <code class="code">$a++</code> or <code class="code">++$a</code>.
    But what if you want to add more than one to it, for instance 3?
    You could write <code class="code">$a++</code> multiple times, but this
    is obviously not a very efficient or comfortable way. A much more
    common practice is to write <code class="code">$a =
    $a + 3</code>. <code class="code">$a + 3</code> 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 <code class="code">$a += 3</code>. This means exactly
    &quot;take the value of <var class="varname">$a</var>, add 3 to it, and assign it
    back into <var class="varname">$a</var>&quot;. In addition to being shorter and
    clearer, this also results in faster execution. The value of
    <code class="code">$a += 3</code>, 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&#039;s
    assigned into <var class="varname">$a</var>). Any two-place operator can be used
    in this operator-assignment mode, for example <code class="code">$a -= 5</code>
    (subtract 5 from the value of <var class="varname">$a</var>), <code class="code">$b *= 7</code>
    (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&#039;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">&lt;?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 />?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
   <p class="para">
    If the value of the first subexpression is <strong><code><a href="reserved.constants.php#constant.true">true</a></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">&lt;?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">?&gt;</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 &#039;<code class="code">expr ;</code>&#039; that is, an
    expression followed by a semicolon. In <code class="code">$b = $a = 5;</code>,
    <code class="code">$a = 5</code> is a valid expression, but it&#039;s not a statement
    by itself. <code class="code">$b = $a = 5;</code>, 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&#039;re
    not interested in the specific value of the expression, but only
    care about whether it means <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
    
    
    
    The constants <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> and <strong><code><a href="reserved.constants.php#constant.false">false</a></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.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&#039;ll write <var class="varname">expr</var>
    to indicate any valid PHP expression.
   </p>
  </div>
<?php manual_footer($setup); ?>