1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
<?xml version="1.0" encoding="utf-8"?>
<sect1 xml:id="language.operators.precedence">
<title>Operator Precedence</title>
<titleabbrev>Operator Precedence</titleabbrev>
<para>
The precedence of an operator specifies how "tightly" it binds two
expressions together. For example, in the expression <literal>1 +
5 * 3</literal>, the answer is <literal>16</literal> and not
<literal>18</literal> because the multiplication ("*") operator
has a higher precedence than the addition ("+") operator.
Parentheses may be used to force precedence, if necessary. For
instance: <literal>(1 + 5) * 3</literal> evaluates to
<literal>18</literal>.
</para>
<para>
When operators have equal precedence their associativity decides
how the operators are grouped. For example "-" is left-associative, so
<literal>1 - 2 - 3</literal> is grouped as <literal>(1 - 2) - 3</literal>
and evaluates to <literal>-4</literal>. "=" on the other hand is
right-associative, so <literal>$a = $b = $c</literal> is grouped as
<literal>$a = ($b = $c)</literal>.
</para>
<para>
Operators of equal precedence that are non-associative cannot be used
next to each other, for example <literal>1 < 2 > 1</literal> is
illegal in PHP. The expression <literal>1 <= 1 == 1</literal> on the
other hand is legal, because the <literal>==</literal> operator has a lower
precedence than the <literal><=</literal> operator.
</para>
<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 <literal>!!$a</literal> can only be grouped as <literal>!(!$a)</literal>.
</para>
<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.
</para>
<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>
<title>Operator Precedence</title>
<tgroup cols="2">
<thead>
<row>
<entry>Associativity</entry>
<entry>Operators</entry>
<entry>Additional Information</entry>
</row>
</thead>
<tbody>
<row>
<entry>(n/a)</entry>
<entry>
<literal>clone</literal>
<literal>new</literal>
</entry>
<entry><link linkend="language.oop5.cloning">clone</link> and <link linkend="language.oop5.basic.new">new</link></entry>
</row>
<row>
<entry>right</entry>
<entry><literal>**</literal></entry>
<entry><link linkend="language.operators.arithmetic">arithmetic</link></entry>
</row>
<row>
<entry>(n/a)</entry>
<entry>
<literal>+</literal>
<literal>-</literal>
<literal>++</literal>
<literal>--</literal>
<literal>~</literal>
<literal>(int)</literal>
<literal>(float)</literal>
<literal>(string)</literal>
<literal>(array)</literal>
<literal>(object)</literal>
<literal>(bool)</literal>
<literal>@</literal>
</entry>
<entry>
<link linkend="language.operators.arithmetic">arithmetic</link> (unary <literal>+</literal> and <literal>-</literal>),
<link linkend="language.operators.increment">increment/decrement</link>,
<link linkend="language.operators.bitwise">bitwise</link>,
<link linkend="language.types.typecasting">type casting</link>&listendand;
<link linkend="language.operators.errorcontrol">error control</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>instanceof</literal></entry>
<entry>
<link linkend="language.operators.type">type</link>
</entry>
</row>
<row>
<entry>(n/a)</entry>
<entry><literal>!</literal></entry>
<entry>
<link linkend="language.operators.logical">logical</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry>
<literal>*</literal>
<literal>/</literal>
<literal>%</literal>
</entry>
<entry>
<link linkend="language.operators.arithmetic">arithmetic</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry>
<literal>+</literal>
<literal>-</literal>
<literal>.</literal>
</entry>
<entry>
<link linkend="language.operators.arithmetic">arithmetic</link> (binary <literal>+</literal> and <literal>-</literal>),
<link linkend="language.operators.array">array</link>&listendand;
<link linkend="language.operators.string">string</link> (<literal>.</literal> prior to PHP 8.0.0)
</entry>
</row>
<row>
<entry>left</entry>
<entry>
<literal><<</literal>
<literal>>></literal>
</entry>
<entry>
<link linkend="language.operators.bitwise">bitwise</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>.</literal></entry>
<entry>
<link linkend="language.operators.string">string</link> (as of PHP 8.0.0)
</entry>
</row>
<row>
<entry>non-associative</entry>
<entry>
<literal><</literal>
<literal><=</literal>
<literal>></literal>
<literal>>=</literal>
</entry>
<entry>
<link linkend="language.operators.comparison">comparison</link>
</entry>
</row>
<row>
<entry>non-associative</entry>
<entry>
<literal>==</literal>
<literal>!=</literal>
<literal>===</literal>
<literal>!==</literal>
<literal><></literal>
<literal><=></literal>
</entry>
<entry>
<link linkend="language.operators.comparison">comparison</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>&</literal></entry>
<entry>
<link linkend="language.operators.bitwise">bitwise</link>&listendand;
<link linkend="language.references">references</link></entry>
</row>
<row>
<entry>left</entry>
<entry><literal>^</literal></entry>
<entry>
<link linkend="language.operators.bitwise">bitwise</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>|</literal></entry>
<entry>
<link linkend="language.operators.bitwise">bitwise</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>&&</literal></entry>
<entry>
<link linkend="language.operators.logical">logical</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>||</literal></entry>
<entry>
<link linkend="language.operators.logical">logical</link>
</entry>
</row>
<row>
<entry>right</entry>
<entry><literal>??</literal></entry>
<entry>
<link linkend="language.operators.comparison.coalesce">null coalescing</link>
</entry>
</row>
<row>
<entry>non-associative</entry>
<entry><literal>? :</literal></entry>
<entry>
<link linkend="language.operators.comparison.ternary">ternary</link>
(left-associative prior to PHP 8.0.0)
</entry>
</row>
<row>
<entry>right</entry>
<entry>
<literal>=</literal>
<literal>+=</literal>
<literal>-=</literal>
<literal>*=</literal>
<literal>**=</literal>
<literal>/=</literal>
<literal>.=</literal>
<literal>%=</literal>
<literal>&=</literal>
<literal>|=</literal>
<literal>^=</literal>
<literal><<=</literal>
<literal>>>=</literal>
<literal>??=</literal>
</entry>
<entry>
<link linkend="language.operators.assignment">assignment</link>
</entry>
</row>
<row>
<entry>(n/a)</entry>
<entry><literal>yield from</literal></entry>
<entry>
<link linkend="control-structures.yield.from">yield from</link>
</entry>
</row>
<row>
<entry>(n/a)</entry>
<entry><literal>yield</literal></entry>
<entry>
<link linkend="control-structures.yield">yield</link>
</entry>
</row>
<row>
<entry>(n/a)</entry>
<entry><literal>print</literal></entry>
<entry><function>print</function></entry>
</row>
<row>
<entry>left</entry>
<entry><literal>and</literal></entry>
<entry>
<link linkend="language.operators.logical">logical</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>xor</literal></entry>
<entry>
<link linkend="language.operators.logical">logical</link>
</entry>
</row>
<row>
<entry>left</entry>
<entry><literal>or</literal></entry>
<entry>
<link linkend="language.operators.logical">logical</link>
</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
<example>
<title>Associativity</title>
<programlisting role="php">
<![CDATA[
<?php
$a = 3 * 3 % 5; // (3 * 3) % 5 = 4
// ternary operator associativity differs from C/C++
$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2 (prior to PHP 8.0.0)
$a = 1;
$b = 2;
$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5
?>
]]>
</programlisting>
</example>
</para>
<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.
<example>
<title>Undefined order of evaluation</title>
<programlisting role="php">
<![CDATA[
<?php
$a = 1;
echo $a + $a++; // may print either 2 or 3
$i = 1;
$array[$i] = $i++; // may set either index 1 or 2
?>
]]>
</programlisting>
</example>
<example>
<title><literal>+</literal>, <literal>-</literal> and <literal>.</literal> have the same precedence (prior to PHP 8.0.0)</title>
<programlisting role="php">
<![CDATA[
<?php
$x = 4;
// this line might result in unexpected output:
echo "x minus one equals " . $x-1 . ", or so I hope\n";
// because it is evaluated like this line (prior to PHP 8.0.0):
echo (("x minus one equals " . $x) - 1) . ", or so I hope\n";
// the desired precedence can be enforced by using parentheses:
echo "x minus one equals " . ($x-1) . ", or so I hope\n";
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
-1, or so I hope
-1, or so I hope
x minus one equals 3, or so I hope
]]>
</screen>
</example>
</para>
<note>
<para>
Although <literal>=</literal> has a lower precedence than
most other operators, PHP will still allow expressions
similar to the following: <literal>if (!$a = foo())</literal>,
in which case the return value of <literal>foo()</literal> is
put into <varname>$a</varname>.
</para>
</note>
<sect2 role="changelog">
&reftitle.changelog;
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.0.0</entry>
<entry>
String concatenation (<literal>.</literal>) now has a lower precedence than
arithmetic addition/subtraction (<literal>+</literal> and <literal>-</literal>) and
bitwise shift left/right (<literal><<</literal> and <literal>>></literal>);
previously it had the same precedence as <literal>+</literal> and <literal>-</literal>
and a higher precedence than <literal><<</literal> and <literal>>></literal>.
</entry>
</row>
<row>
<entry>8.0.0</entry>
<entry>
The ternary operator (<literal>? :</literal>) is non-associative now;
previously it was left-associative.
</entry>
</row>
<row>
<entry>7.4.0</entry>
<entry>
Relying on the precedence of string concatenation (<literal>.</literal>) relative to
arithmetic addition/subtraction (<literal>+</literal> or <literal>-</literal>) or
bitwise shift left/right (<literal><<</literal> or <literal>>></literal>),
i.e. using them together in an unparenthesized expression, is deprecated.
</entry>
</row>
<row>
<entry>7.4.0</entry>
<entry>
Relying on left-associativity of the ternary operator (<literal>? :</literal>),
i.e. nesting multiple unparenthesized ternary operators, is deprecated.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</sect2>
</sect1>
|