Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8946

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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>Operator Precedence</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.operators.html">Operators</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.operators.arithmetic.html">Arithmetic Operators</a></div>
 <div class="up"><a href="language.operators.html">Operators</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.operators.precedence" class="sect1">
   <h2 class="title">Operator Precedence</h2>
   <p class="para">
    The precedence of an operator specifies how &quot;tightly&quot; it binds two
    expressions together. For example, in the expression <em>1 +
    5 * 3</em>, the answer is <em>16</em> and not
    <em>18</em> because the multiplication (&quot;*&quot;) operator
    has a higher precedence than the addition (&quot;+&quot;) operator.
    Parentheses may be used to force precedence, if necessary. For
    instance: <em>(1 + 5) * 3</em> evaluates to
    <em>18</em>.
   </p>
   <p class="para">
    When operators have equal precedence their associativity decides
    how the operators are grouped. For example &quot;-&quot; is left-associative, so
    <em>1 - 2 - 3</em> is grouped as <em>(1 - 2) - 3</em>
    and evaluates to <em>-4</em>. &quot;=&quot; on the other hand is
    right-associative, so <em>$a = $b = $c</em> is grouped as
    <em>$a = ($b = $c)</em>.
   </p>
   <p class="para">
    Operators of equal precedence that are non-associative cannot be used
    next to each other, for example <em>1 &lt; 2 &gt; 1</em> is
    illegal in PHP. The expression <em>1 &lt;= 1 == 1</em> on the
    other hand is legal, because the <em>==</em> operator has lesser
    precedence than the <em>&lt;=</em> operator.
   </p>
   <p class="para">
    Use of parentheses, even when not strictly necessary, can often increase
    readability of the code by making grouping explicit rather than relying
    on the implicit operator precedence and associativity.
   </p>
   <p class="para">
    The following table lists the operators in order of precedence, with
    the highest-precedence ones at the top. Operators on the same line
    have equal precedence, in which case associativity decides grouping.
    <table class="doctable table">
     <caption><strong>Operator Precedence</strong></caption>
     
      <thead>
       <tr>
        <th>Associativity</th>
        <th>Operators</th>
        <th>Additional Information</th>
       </tr>

      </thead>

      <tbody class="tbody">
       <tr>
        <td>non-associative</td>
        <td>
         <em>clone</em>
         <em>new</em>
        </td>
        <td><a href="language.oop5.cloning.html" class="link">clone</a> and <a href="language.oop5.basic.html#language.oop5.basic.new" class="link">new</a></td>
       </tr>

       <tr>
        <td>left</td>
        <td><em>[</em></td>
        <td> <span class="function"><a href="function.array.html" class="function">array()</a></span></td>
       </tr>

       <tr>
        <td>right</td>
        <td>
         <em>++</em> 
         <em>--</em> 
         <em>~</em> 
         <em>(int)</em> 
         <em>(float)</em> 
         <em>(string)</em> 
         <em>(array)</em>
         <em>(object)</em> 
         <em>(bool)</em> 
         <em>@</em>
        </td>
        <td>
         <a href="language.types.html" class="link">types</a> and <a href="language.operators.increment.html" class="link">increment/decrement</a>
        </td>
       </tr>

       <tr>
        <td>non-associative</td>
        <td><em>instanceof</em></td>
        <td>
         <a href="language.types.html" class="link">types</a>
        </td>
       </tr>

       <tr>
        <td>right</td>
        <td><em>!</em></td>
        <td>
         <a href="language.operators.logical.html" class="link">logical</a>
        </td>
       </tr>

       <tr>
        <td>left</td>
        <td>
         <em>*</em>
         <em>/</em>
         <em>%</em>
        </td>
        <td>
         <a href="language.operators.arithmetic.html" class="link">arithmetic</a>
        </td>
       </tr>

       <tr>
        <td>left</td>
        <td>
         <em>+</em> 
         <em>-</em> 
         <em>.</em>
        </td>
        <td>
         <a href="language.operators.arithmetic.html" class="link">arithmetic</a> and
         <a href="language.operators.string.html" class="link">string</a></td>
       </tr>

       <tr>
        <td>left</td>
        <td>
         <em>&lt;&lt;</em> 
         <em>&gt;&gt;</em>
        </td>
        <td>
         <a href="language.operators.bitwise.html" class="link">bitwise</a>
        </td>
       </tr>

       <tr>
        <td>non-associative</td>
        <td>
         <em>&lt;</em> 
         <em>&lt;=</em> 
         <em>&gt;</em> 
         <em>&gt;=</em>
        </td>
        <td>
         <a href="language.operators.comparison.html" class="link">comparison</a>
        </td>
       </tr>

       <tr>
        <td>non-associative</td>
        <td>
         <em>==</em>
         <em>!=</em> 
         <em>===</em> 
         <em>!==</em> 
         <em>&lt;&gt;</em>
        </td>
        <td>
         <a href="language.operators.comparison.html" class="link">comparison</a>
        </td>
       </tr>

       <tr>
        <td>left</td>
        <td><em>&amp;</em></td>
        <td>
         <a href="language.operators.bitwise.html" class="link">bitwise</a> and
         <a href="language.references.html" class="link">references</a></td>
       </tr>

       <tr>
        <td>left</td>
        <td><em>^</em></td>
        <td>
         <a href="language.operators.bitwise.html" class="link">bitwise</a>
        </td>
       </tr>

       <tr>
        <td>left</td>
        <td><em>|</em></td>
        <td>
         <a href="language.operators.bitwise.html" class="link">bitwise</a>
        </td>
       </tr>

       <tr>
        <td>left</td>
        <td><em>&amp;&amp;</em></td>
        <td>
         <a href="language.operators.logical.html" class="link">logical</a>
        </td>
       </tr>

       <tr>
        <td>left</td>
        <td><em>||</em></td>
        <td>
         <a href="language.operators.logical.html" class="link">logical</a>
        </td>
       </tr>

       <tr>
        <td>left</td>
        <td><em>? :</em></td>
        <td>
         <a href="language.operators.comparison.html#language.operators.comparison.ternary" class="link">ternary</a>
        </td>
       </tr>

       <tr>
        <td>right</td>
        <td>
         <em>=</em> 
         <em>+=</em> 
         <em>-=</em> 
         <em>*=</em> 
         <em>/=</em> 
         <em>.=</em> 
         <em>%=</em> 
         <em>&amp;=</em> 
         <em>|=</em> 
         <em>^=</em> 
         <em>&lt;&lt;=</em> 
         <em>&gt;&gt;=</em>
         <em>=&gt;</em>
        </td>
        <td>
         <a href="language.operators.assignment.html" class="link">assignment</a>
        </td>
       </tr>

       <tr>
        <td>left</td>
        <td><em>and</em></td>
        <td>
         <a href="language.operators.logical.html" class="link">logical</a>
        </td>
       </tr>

       <tr>
        <td>left</td>
        <td><em>xor</em></td>
        <td>
         <a href="language.operators.logical.html" class="link">logical</a>
        </td>
       </tr>

       <tr>
        <td>left</td>
        <td><em>or</em></td>
        <td>
         <a href="language.operators.logical.html" class="link">logical</a>
        </td>
       </tr>

       <tr>
        <td>left</td>
        <td><em>,</em></td>
        <td>many uses</td>
       </tr>

      </tbody>
     
    </table>

   </p>
   <p class="para">
    <div class="example" id="example-115">
     <p><strong>Example #1 Associativity</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">%&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;(3&nbsp;*&nbsp;3)&nbsp;%&nbsp;5&nbsp;=&nbsp;4<br />//&nbsp;ternary&nbsp;operator&nbsp;associativity&nbsp;differs&nbsp;from&nbsp;C/C++<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;(true&nbsp;?&nbsp;0&nbsp;:&nbsp;true)&nbsp;?&nbsp;1&nbsp;:&nbsp;2&nbsp;=&nbsp;2<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$a&nbsp;=&nbsp;($b&nbsp;+=&nbsp;3)&nbsp;-&gt;&nbsp;$a&nbsp;=&nbsp;5,&nbsp;$b&nbsp;=&nbsp;5<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    Operator precedence and associativity only determine how expressions
    are grouped, they do not specify an order of evaluation. PHP does not
    (in the general case) specify in which order an expression is evaluated
    and code that assumes a specific order of evaluation should be avoided,
    because the behavior can change between versions of PHP or depending on
    the surrounding code.
    <div class="example" id="example-116">
     <p><strong>Example #2 Undefined order of evaluation</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">++;&nbsp;</span><span style="color: #FF8000">//&nbsp;may&nbsp;print&nbsp;either&nbsp;2&nbsp;or&nbsp;3<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++;&nbsp;</span><span style="color: #FF8000">//&nbsp;may&nbsp;set&nbsp;either&nbsp;index&nbsp;1&nbsp;or&nbsp;2<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Although <em>=</em> has a lower precedence than
     most other operators, PHP will still allow expressions
     similar to the following: <em>if (!$a = foo())</em>,
     in which case the return value of <em>foo()</em> is
     put into <var class="varname"><var class="varname">$a</var></var>.
    </p>
   </p></blockquote>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.operators.html">Operators</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.operators.arithmetic.html">Arithmetic Operators</a></div>
 <div class="up"><a href="language.operators.html">Operators</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>