<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.operators.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.operators.precedence.php',
    1 => 'Operator Precedence',
    2 => 'Operator Precedence',
  ),
  'up' => 
  array (
    0 => 'language.operators.php',
    1 => 'Operators',
  ),
  'prev' => 
  array (
    0 => 'language.operators.php',
    1 => 'Operators',
  ),
  'next' => 
  array (
    0 => 'language.operators.arithmetic.php',
    1 => 'Arithmetic',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/operators/precedence.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<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 <code class="literal">1 +
  5 * 3</code>, the answer is <code class="literal">16</code> and not
  <code class="literal">18</code> 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: <code class="literal">(1 + 5) * 3</code> evaluates to
  <code class="literal">18</code>.
 </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
  <code class="literal">1 - 2 - 3</code> is grouped as <code class="literal">(1 - 2) - 3</code>
  and evaluates to <code class="literal">-4</code>. &quot;=&quot; on the other hand is
  right-associative, so <code class="literal">$a = $b = $c</code> is grouped as
  <code class="literal">$a = ($b = $c)</code>.
 </p>
 <p class="para">
  Operators of equal precedence that are non-associative cannot be used
  next to each other, for example <code class="literal">1 &lt; 2 &gt; 1</code> is
  illegal in PHP. The expression <code class="literal">1 &lt;= 1 == 1</code> on the
  other hand is legal, because the <code class="literal">==</code> operator has a lower
  precedence than the <code class="literal">&lt;=</code> operator.
 </p>
 <p class="para">
  Associativity is only meaningful for binary (and ternary) operators.
  Unary operators are either prefix or postfix so this notion is not applicable.
  For example <code class="literal">!!$a</code> can only be grouped as <code class="literal">!(!$a)</code>.
 </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>(n/a)</td>
      <td>
       <code class="literal">clone</code>
       <code class="literal">new</code>
      </td>
      <td><a href="language.oop5.cloning.php" class="link">clone</a> and <a href="language.oop5.basic.php#language.oop5.basic.new" class="link">new</a></td>
     </tr>

     <tr>
      <td>right</td>
      <td><code class="literal">**</code></td>
      <td><a href="language.operators.arithmetic.php" class="link">arithmetic</a></td>
     </tr>

     <tr>
      <td>(n/a)</td>
      <td>
       <code class="literal">+</code>
       <code class="literal">-</code>
       <code class="literal">++</code>
       <code class="literal">--</code>
       <code class="literal">~</code>
       <code class="literal">(int)</code>
       <code class="literal">(float)</code>
       <code class="literal">(string)</code>
       <code class="literal">(array)</code>
       <code class="literal">(object)</code>
       <code class="literal">(bool)</code>
       <code class="literal">@</code>
      </td>
      <td>
       <a href="language.operators.arithmetic.php" class="link">arithmetic</a> (unary <code class="literal">+</code> and <code class="literal">-</code>),
       <a href="language.operators.increment.php" class="link">increment/decrement</a>,
       <a href="language.operators.bitwise.php" class="link">bitwise</a>,
       <a href="language.types.type-juggling.php#language.types.typecasting" class="link">type casting</a> and
       <a href="language.operators.errorcontrol.php" class="link">error control</a>
      </td>
     </tr>

     <tr>
      <td>left</td>
      <td><code class="literal">instanceof</code></td>
      <td>
       <a href="language.operators.type.php" class="link">type</a>
      </td>
     </tr>

     <tr>
      <td>(n/a)</td>
      <td><code class="literal">!</code></td>
      <td>
       <a href="language.operators.logical.php" class="link">logical</a>
      </td>
     </tr>

     <tr>
      <td>left</td>
      <td>
       <code class="literal">*</code>
       <code class="literal">/</code>
       <code class="literal">%</code>
      </td>
      <td>
       <a href="language.operators.arithmetic.php" class="link">arithmetic</a>
      </td>
     </tr>

     <tr>
      <td>left</td>
      <td>
       <code class="literal">+</code>
       <code class="literal">-</code>
       <code class="literal">.</code>
      </td>
      <td>
       <a href="language.operators.arithmetic.php" class="link">arithmetic</a> (binary <code class="literal">+</code> and <code class="literal">-</code>),
       <a href="language.operators.array.php" class="link">array</a> and
       <a href="language.operators.string.php" class="link">string</a> (<code class="literal">.</code> prior to PHP 8.0.0)
      </td>
     </tr>

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

     <tr>
      <td>left</td>
      <td><code class="literal">.</code></td>
      <td>
       <a href="language.operators.string.php" class="link">string</a> (as of PHP 8.0.0)
      </td>
     </tr>

     <tr>
      <td>left</td>
      <td><code class="literal">|&gt;</code></td>
      <td>
       <a href="language.operators.functional.php" class="link">pipe</a>
      </td>
     </tr>

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

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

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

     <tr>
      <td>left</td>
      <td><code class="literal">^</code></td>
      <td>
       <a href="language.operators.bitwise.php" class="link">bitwise</a>
      </td>
     </tr>

     <tr>
      <td>left</td>
      <td><code class="literal">|</code></td>
      <td>
       <a href="language.operators.bitwise.php" class="link">bitwise</a>
      </td>
     </tr>

     <tr>
      <td>left</td>
      <td><code class="literal">&amp;&amp;</code></td>
      <td>
       <a href="language.operators.logical.php" class="link">logical</a>
      </td>
     </tr>

     <tr>
      <td>left</td>
      <td><code class="literal">||</code></td>
      <td>
       <a href="language.operators.logical.php" class="link">logical</a>
      </td>
     </tr>

     <tr>
      <td>right</td>
      <td><code class="literal">??</code></td>
      <td>
       <a href="language.operators.comparison.php#language.operators.comparison.coalesce" class="link">null coalescing</a>
      </td>
     </tr>

     <tr>
      <td>non-associative</td>
      <td><code class="literal">? :</code></td>
      <td>
       <a href="language.operators.comparison.php#language.operators.comparison.ternary" class="link">ternary</a>
       (left-associative prior to PHP 8.0.0)
      </td>
     </tr>

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

     <tr>
      <td>(n/a)</td>
      <td><code class="literal">yield from</code></td>
      <td>
       <a href="language.generators.syntax.php#control-structures.yield.from" class="link">yield from</a>
      </td>
     </tr>

     <tr>
      <td>(n/a)</td>
      <td><code class="literal">yield</code></td>
      <td>
       <a href="language.generators.syntax.php#control-structures.yield" class="link">yield</a>
      </td>
     </tr>

     <tr>
      <td>(n/a)</td>
      <td><code class="literal">print</code></td>
      <td><span class="function"><a href="function.print.php" class="function">print</a></span></td>
     </tr>

     <tr>
      <td>left</td>
      <td><code class="literal">and</code></td>
      <td>
       <a href="language.operators.logical.php" class="link">logical</a>
      </td>
     </tr>

     <tr>
      <td>left</td>
      <td><code class="literal">xor</code></td>
      <td>
       <a href="language.operators.logical.php" class="link">logical</a>
      </td>
     </tr>

     <tr>
      <td>left</td>
      <td><code class="literal">or</code></td>
      <td>
       <a href="language.operators.logical.php" class="link">logical</a>
      </td>
     </tr>

     <tr>
      <td>(n/a)</td>
      <td><code class="literal">throw</code></td>
      <td>
       <a href="language.exceptions.php" class="link">throw</a>
      </td>
     </tr>

    </tbody>
   
  </table>

 </p>
 <p class="para">
  <div class="example" id="example-1">
   <p><strong>Example #1 Associativity</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">3 </span><span style="color: #007700">* </span><span style="color: #0000BB">3 </span><span style="color: #007700">% </span><span style="color: #0000BB">5</span><span style="color: #007700">; </span><span style="color: #FF8000">// (3 * 3) % 5 = 4<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">$b </span><span style="color: #007700">+= </span><span style="color: #0000BB">3</span><span style="color: #007700">; </span><span style="color: #FF8000">// $a = ($b += 3) -&gt; $a = 5, $b = 5<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p>
 <p class="para">
  The ternary operator specifically requires the use of parenthesis to
  disambiguate precedence.
 </p>
 <p class="para">
  <div class="example" id="example-2">
   <p><strong>Example #2 Explicit Precedence</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">true </span><span style="color: #007700">? </span><span style="color: #0000BB">0 </span><span style="color: #007700">: (</span><span style="color: #0000BB">true </span><span style="color: #007700">? </span><span style="color: #0000BB">1 </span><span style="color: #007700">: </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// this is not allowed since PHP 8<br />// $a = true ? 0 : true ? 1 : 2;<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-3">
   <p><strong>Example #3 Undefined order of evaluation</strong></p>
   <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$a</span><span style="color: #007700">++; </span><span style="color: #FF8000">// may print either 2 or 3<br /><br /></span><span style="color: #0000BB">$i </span><span style="color: #007700">= </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">] = </span><span style="color: #0000BB">$i</span><span style="color: #007700">++; </span><span style="color: #FF8000">// may set either index 1 or 2<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
  <div class="example" id="example-4">
   <p><strong>Example #4 <code class="literal">+</code>, <code class="literal">-</code> and <code class="literal">.</code> precedence</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$x </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// this line might result in unexpected output:<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"x minus one equals " </span><span style="color: #007700">. </span><span style="color: #0000BB">$x</span><span style="color: #007700">-</span><span style="color: #0000BB">1 </span><span style="color: #007700">. </span><span style="color: #DD0000">", or so I hope\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// the desired precedence can be enforced by using parentheses:<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"x minus one equals " </span><span style="color: #007700">. (</span><span style="color: #0000BB">$x</span><span style="color: #007700">-</span><span style="color: #0000BB">1</span><span style="color: #007700">) . </span><span style="color: #DD0000">", or so I hope\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// this is not allowed, and throws a TypeError:<br /></span><span style="color: #007700">echo ((</span><span style="color: #DD0000">"x minus one equals " </span><span style="color: #007700">. </span><span style="color: #0000BB">$x</span><span style="color: #007700">) - </span><span style="color: #0000BB">1</span><span style="color: #007700">) . </span><span style="color: #DD0000">", or so I hope\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
-1, or so I hope
-1, or so I hope
Fatal error: Uncaught TypeError: Unsupported operand types: string - int
</pre></div>
   </div>
  </div>
  <div class="example" id="example-5">
   <p><strong>Example #5 Prior to PHP 8, <code class="literal">+</code>, <code class="literal">-</code> and <code class="literal">.</code> had the same precedence</strong></p>
   <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$x </span><span style="color: #007700">= </span><span style="color: #0000BB">4</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// this line might result in unexpected output:<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"x minus one equals " </span><span style="color: #007700">. </span><span style="color: #0000BB">$x</span><span style="color: #007700">-</span><span style="color: #0000BB">1 </span><span style="color: #007700">. </span><span style="color: #DD0000">", or so I hope\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// because it is evaluated like this line (prior to PHP 8.0.0):<br /></span><span style="color: #007700">echo ((</span><span style="color: #DD0000">"x minus one equals " </span><span style="color: #007700">. </span><span style="color: #0000BB">$x</span><span style="color: #007700">) - </span><span style="color: #0000BB">1</span><span style="color: #007700">) . </span><span style="color: #DD0000">", or so I hope\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// the desired precedence can be enforced by using parentheses:<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"x minus one equals " </span><span style="color: #007700">. (</span><span style="color: #0000BB">$x</span><span style="color: #007700">-</span><span style="color: #0000BB">1</span><span style="color: #007700">) . </span><span style="color: #DD0000">", or so I hope\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
-1, or so I hope
-1, or so I hope
x minus one equals 3, or so I hope
</pre></div>
   </div>
  </div>
 </p>
 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   Although <code class="literal">=</code> has a lower precedence than
   most other operators, PHP will still allow expressions
   similar to the following: <code class="literal">if (!$a = foo())</code>,
   in which case the return value of <code class="literal">foo()</code> is
   put into <var class="varname">$a</var>.
  </p>
 </p></blockquote>
 <div class="sect2">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       String concatenation (<code class="literal">.</code>) now has a lower precedence than
       arithmetic addition/subtraction (<code class="literal">+</code> and <code class="literal">-</code>) and
       bitwise shift left/right (<code class="literal">&lt;&lt;</code> and <code class="literal">&gt;&gt;</code>);
       previously it had the same precedence as <code class="literal">+</code> and <code class="literal">-</code>
       and a higher precedence than <code class="literal">&lt;&lt;</code> and <code class="literal">&gt;&gt;</code>.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       The ternary operator (<code class="literal">? :</code>) is non-associative now;
       previously it was left-associative.
      </td>
     </tr>

     <tr>
      <td>7.4.0</td>
      <td>
       Relying on the precedence of string concatenation (<code class="literal">.</code>) relative to
       arithmetic addition/subtraction (<code class="literal">+</code> or <code class="literal">-</code>) or
       bitwise shift left/right (<code class="literal">&lt;&lt;</code> or <code class="literal">&gt;&gt;</code>),
       i.e. using them together in an unparenthesized expression, is deprecated.
      </td>
     </tr>

     <tr>
      <td>7.4.0</td>
      <td>
       Relying on left-associativity of the ternary operator (<code class="literal">? :</code>),
       i.e. nesting multiple unparenthesized ternary operators, is deprecated.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>
</div><?php manual_footer($setup); ?>