PHP learning notes operator priority _ PHP Tutorial

Source: Internet
Author: User
Tags php operator
PHP learning note operator priority. Operator priority is an important knowledge point that a programmer must understand and hold. let's take a look at the article on PHP operator priority. Today, the operator priority in Lao Wang's technical manual is an important knowledge point that programmers must understand and hold. let's take a look at the PHP operator priority article.

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

The code is as follows:

What is output?

This question may seem simple at first glance, but it is not easy to think carefully,

If Boolean and the previous part are due to priority issues, but if it is only a matter of priority, the result should be:

The code is as follows:
$ A = (100 & $ B) = 200

In fact, the result is indeed a high-priority & concession to the next priority =, let $ B = 200 first combined.

The reason is that PHP does not fully comply with the definition of priority, which is also described in the PHP manual:

Note: Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (! $ A = foo (), in which case the return value of foo () is put into $.

In this design, I personally do not express my opinion. in C language, such similar statements are determined as incorrect syntax. PHP adopts such a design, which may be due to historical reasons,

Curious people may want to know why. Previously, jayeeliu asked:

Hello laruence:

Question about the php operator priority

The code is as follows:

$ T = 1 & $ tt = 2

According to the php operator, the priority should be

The code is as follows:

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

This sequence is executed, but it should actually be

The code is as follows:

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

I don't quite understand it.

In fact, it is also simple. operator priority is a method for selecting protocol rules when there is a semantic grammar. in the syntax analysis file definition of PHP, so that there is no protocol conflict between the equal sign and T_BOOLEAN_AND:

The code is as follows:

Expr_without_variable:
// A hidden rule exists, which is equivalent to T_BOOLEAN_AND becoming the "unary operator ".
| Expr T_BOOLEAN_AND {zend_do_boolean_and_begin (& $1, & $2 TSRMLS_CC);} exp

Finally, by the way, PHP also defines T_LOGICAL_AND (and) and T_LOGICAL_OR (or) for T_BOOLEAN_AND. the priority of these two values is lower than the equal sign, so there will be, the following are typical examples of PHP tutorials:

The code is as follows:

$ Result = mysql_query (*) or die (mysql_error ());

Similarly, we can use or to implement the ternary operator (? :) Features:

The code is as follows:

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

// Equivalent:

The code is as follows:

$ Person = empty ($ who )? "Laruence": $ who;

Integration direction

Operator
Left ,
Left Or
Left Xor
Left And
Right Print
Right = + =-= * =/=. = % = & = | = ^ = ~ = <=> =
Left ? :
Left |
Left &&
Left |
Left ^
Left &
None =! ===! =
None <=> =
Left <>
Left + -.
Left */%
Right ! ~ ++ -- (Int) (float) (string) (array) (object )@
Right [
None New

Bytes. Today's old Wang 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.