[DOC-CVS] [doc-en] master: doc: clarify unary-vs-instanceof precedence and assignment binding inside prefix operators (#5627)

[email protected] (Seifeddine Gmati via GitHub) Fri, 19 Jun 2026 03:21:07 +0000
Newsgroups php.doc.cvs
Message-ID <[email protected]>
Author: Seifeddine Gmati (azjezz)
Committer: GitHub (web-flow)
Pusher: philip
Date: 2026-06-18T20:21:05-07:00

Commit: https://github.com/php/doc-en/commit/80dfa568e602649eb13585bf59a7b6239eddaac3
Raw diff: https://github.com/php/doc-en/commit/80dfa568e602649eb13585bf59a7b6239eddaac3.diff

doc: clarify unary-vs-instanceof precedence and assignment binding inside prefix operators (#5627)

Changed paths:
  M  language/operators/precedence.xml


Diff:

diff --git a/language/operators/precedence.xml b/language/operators/precedence.xml
index 6808a8daa162..35ac34c0cb7a 100644
--- a/language/operators/precedence.xml
+++ b/language/operators/precedence.xml
@@ -300,6 +300,18 @@
    </tgroup>
   </table>
  </para>
+ <note>
+  <simpara>
+   The unary operators sharing the row with the cast operators
+   (<literal>~</literal>, <literal>@</literal>, and unary
+   <literal>+</literal>/<literal>-</literal>) bind more tightly than
+   <literal>instanceof</literal>, whereas <literal>!</literal> binds less
+   tightly. Consequently <literal>(int) $x instanceof Foo</literal> is grouped
+   as <literal>((int) $x) instanceof Foo</literal>, while
+   <literal>!$x instanceof Foo</literal> is grouped as
+   <literal>!($x instanceof Foo)</literal>.
+  </simpara>
+ </note>
  <para>
   <example>
    <title>Associativity</title>
@@ -420,6 +432,15 @@ x minus one equals 3, or so I hope
    in which case the return value of <literal>foo()</literal> is
    put into <varname>$a</varname>.
   </para>
+  <simpara>
+   This is possible because the left-hand side of an assignment must be a
+   variable, so the assignment is grouped with that variable rather than with
+   the surrounding higher-precedence prefix operator. The same applies to the
+   other prefix operators that take an expression operand, such as
+   <literal>clone</literal>, the cast operators, <literal>@</literal> and
+   <literal>~</literal>: for instance <literal>clone $a = $b</literal> is grouped
+   as <literal>clone ($a = $b)</literal>, not <literal>(clone $a) = $b</literal>.
+  </simpara>
  </note>
  <sect2 role="changelog">
   &reftitle.changelog;