PHP Learning Note Operator Precedence _php tutorial

Source: Internet
Author: User
Tags php operator
Operator Precedence is an important point of knowledge that a programmer must understand and hold, let's take a look at the PHP operator precedence article.

Today in Lao Wang's technical manual saw a problem:

The code is as follows Copy Code

What is the output?

This is a question that may seem simple at first glance, but in fact it is not easy to scrutinize

If the Boolean and the previous part are due to the priority problem, but if the problem is only a priority, then the result should be:

The code is as follows Copy Code
$a = (&& $b) = 200

and the actual result, really high priority && concession to the sub-priority =, let $b = 200 first combined.

The reason for this is that PHP does not fully adhere to the definition of priority, which is also explained in the PHP manual:

Note:although = have a lower precedence than most other operators, PHP would still allow expressions similar to the FOLLOWI Ng:if (! $a = foo ()), in which case the return value of Foo () was put into $a.

Such a design, the individual does not express views, anyway in C language, so similar statements are judged to be grammatical error. PHP uses this design, which is probably the historical reason,

There are curious classmates, will want to know exactly why, before Jayeeliu netizens also asked:

Laruence Hello:

Ask a PHP operator for priority issues

The code is as follows Copy Code

$t = = 1 && $tt = 2

The precedence of the PHP operator should be

The code is as follows Copy Code

(($t = = 1) && $tt) = 2

This order is executed, but it should actually be

The code is as follows Copy Code

($t = = 1) && ($tt = 2)

I don't understand a bit.

In fact, operator precedence is a means of choosing a protocol rule in the presence of ambiguity grammars, while PHP's parsing file definition does not have a statute conflict before the equals sign and T_boolean_and (&&):

The code is as follows Copy Code

Expr_without_variable:
There are hidden rules that are equivalent to T_boolean_and becoming "unary operators".
| Expr T_boolean_and {zend_do_boolean_and_begin (&$1, &$2 tsrmls_cc);} exp

Finally, by the way, PHP corresponds to T_boolean_and also defines T_logical_and (and) and t_logical_or (or), both of which have lower precedence than equals sign, so there will be, a lot of PHP introductory textbook sample code in the classic:

The code is as follows Copy Code

$result = mysql_query (*) or Die (Mysql_error ());

Similarly, you can use or to implement ternary operators (?:) Features of:

The code is as follows Copy Code

$person = $who or $person = "laruence";

Equivalent to:

The code is as follows Copy Code

$person = Empty ($who)? "Laruence": $who;

Combination direction

operator
Left ,
Left Or
Left Xor
Left and
Right Print
Right = + = = *=/=. =%= &= |= ^= ~= <<= >>=
Left ? :
Left ||
Left &&
Left |
Left ^
Left &
No == != === !==
No < <= > >=
Left << >>
Left + - .
Left * / %
Right ! ~ + +--(int) (float) (string) (array) (object) @
Right [
No New

http://www.bkjia.com/PHPjc/632626.html www.bkjia.com true http://www.bkjia.com/PHPjc/632626.html techarticle operator Precedence is an important point of knowledge that a programmer must understand and hold, let's take a look at the PHP operator precedence article. Today in Lao Wang's technical manual ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.