PHP has 5 arithmetic operators (+-*/%), 6 assignment operators (+ = = *=/=%=. =), 8 comparison operators (= = = < > <= >=! = <>!==), and 6 logical operators (such as &&, | |, &, |,!. Where logic and & can be written and, logically or | written or, and some other operators (error control operator @, execute operator anti-quote "-not single quotes, string operators.) ++/--、 ternary operator with. =, increment/decrement operator?:).
For operator Precedence, refer to the relevant documentation.
The similarities and differences between & and && in PHP
[PHP]View PlainCopy
- <?php
- $a = 10;
- if ($a >4 && (+$a >10))
- {
- }
- The output result is 11.
- echo $a;
- ?>
- <?php
- $a = 10;
- if ($a >4 and (+$a >10))
- {
- }
- The output result is 11.
- echo $a;
- ?>
- **************************************************************
- <?php
- $a = 10;
- if ($a >4 && (+$a <10))
- {
- }
- The output result is 11.
- echo $a;
- ?>
- <?php
- $a = 10;
- if ($a >4 & (+$a <10))
- {
- }
- The output result is 11.
- echo $a;
- ?>
- *********************************************************
- <?php
- $a = 10;
- if ($a <4 && (+$a >10))
- {
- }
- The output result is 10.
- echo $a;
- ?>
- <?php
- $a = 10;
- if ($a <4 & (+$a >10))
- {
- }
- The output result is 11.
- echo $a;
- ?>
- *******************************************************************
- <?php
- $a = 10;
- if ($a <4 && (+$a <10))
- {
- }
- The output result is 10.
- echo $a;
- ?>
- <?php
- $a = 10;
- if ($a <4 & (+$a <10))
- {
- }
- The output result is 11.
- echo $a;
- ?>
- *******************************************************************
- <?php
- The following sktest () are not called because they are "shorted" by the operator.
- $a = (false && sktest ());
- $b = (true | | | sktest ());
- $c = (False and sktest ());
- $d = (True or sktest ());
- "| |" has a higher priority than "or"
- $e = False | | True //$e is assigned (false | | true) and the result is true
- $f = False or true; //$f is assigned false [Altair NOTE: ' = ' has a higher precedence than ' or ']
- Var_dump ($e, $f);
- "&&" has a higher priority than "and"
- $g = True && false; //$g assigned value (True && false), False
- $h = True and false; //$h is assigned true [Altair Note: ' = ' has a higher priority than ' and ']
- Var_dump ($g, $h);
- ?>
Expression A $ A && $b, expression two $ A & $b
1, the same point: two expressions are when $ A, $b are true, the expression is true. The two operators have no effect on the result of this expression.
2, different points: the expression of $ A && $b && before the $ A is false, determined that the expression is false, the logical operator && after the $b is no longer evaluated, that is, the so-called operator "short circuit". And for the expression $ A & $b, regardless of the & before the true and false, the expression after the $b still to be calculated. Whether the $b after the logical operator is run may affect subsequent data and program results.
For logical operators | | and |, the similarities and differences ibid.
The similarities and differences between & and && in PHP logic operators