Today in Lao Wang's technical handbook see a problem:
The code is as follows |
Copy Code |
<?PHPIF ($a = && $b =) {Var_dump ($a, $b);} |
What is the output?
This question may seem simple at first glance, but it is not easy to scrutinize it,
If the Boolean and previous portions are due to priority issues, but if it 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 && concessions to the second priority =, let $b = 200 first combined.
The reason for this is that PHP does not fully comply with the priority definition, which is also described in the PHP manual:
Note:although = has a lower precedence than most other operators, PHP would still allow the expressions to the similar Ng:if (! $a = foo ()), in which case the return value of Foo () was put into $a.
Such a design, the individual does not express opinion, anyway in C language, such a similar statement is judged to be grammatically incorrect. PHP uses this kind of design, most likely is the historical reason,
Have curious classmate, will want to know exactly why, before Jayeeliu Netizen also asked:
Laruence Hello:
Ask a question about PHP operator precedence
The code is as follows |
Copy Code |
$t = = 1 && $tt = 2 |
According to the PHP operator precedence 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 quite understand.
In fact, operator precedence is a means of choosing a protocol rule in the presence of ambiguity grammars, and in PHP's parsing file definition, there is no statute conflict between the equals sign and T_boolean_and (&&):
The code is as follows |
Copy Code |
Expr_without_variable: The existence of a hidden rule, equivalent to T_boolean_and become a "unary operator." | 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, so there's a lot of the classic PHP introductory textbook sample code:
The code is as follows |
Copy Code |
$result = mysql_query (*) or Die (Mysql_error ()); |
Similarly, you can use or to implement the ternary operator (?:) The function:
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; |
Combining 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 |