Logical operations of php basic syntax
Logical operators are relatively simple and are a way for us humans to think logically.
Tell me the wish of many men wearing hanging silk: If a certain woman is beautiful or richer than me, I will marry her. If none of the conditions are met, forget it.
The above mental state of not evaluating good or bad is just to illustrate that this is typical computer thinking.
If the condition of beauty is true (true) or the condition of wealth is true (true), then the behavior and action of marrying her will be performed. Otherwise, don’t marry this girl.
Then we have summarized and summarized these logics. In the table below: $x is condition one and $y is condition two. Explanation:
Logical AND, interpreted in Chinese as AND. It can be understood that it is executed when $x and $y are both true.
Logical OR, interpreted as OR in Chinese. It can be understood as executing when either $x or $y is true.
Logical negation, Chinese explanation is inversion. If $x is false, perform a non-operation. If it is not false (false), it is true, and the true interval can be executed. If true is inferred, the false interval will be executed.
Logical XOR, if $x and $y are the same, it is false, if they are not the same, it is true.
Example | Explanation | Detailed explanation |
---|---|---|
$ x and $y | Logical AND (and relationship) | Returns true if $x and $y are true |
$x && $y | Same as above | Same as above |
$x or $y | Logical or | $x,$y both It is false when it is false, and all other cases are true |
$a||$b | Same as above | Same as above |
!$x | Logical not | Inversion, that is, true becomes false, false becomes true |
$x xor $y | Logical exclusive OR | If they are the same, false is the same, if they are different, the true is true |
Then let’s give a few examples to try. You should also remember to do more experiments yourself (you can combine the comparison operators in chapter 3.4.4 to write a few examples yourself).
Logical AND:
Logical OR:
Logical NOT:
【Key Knowledge】Short circuit
Short circuiting is to think in a lazy mode.
The characteristic of logical AND is: if both sides are true, it is true, and other situations are false.
The characteristics of logical OR are: if both sides are false, both are false, and all other situations are true.
We now imagine ourselves as a lazy man, very, very lazy. Let's think about logical AND and logical OR. Can it be understood this way:
Logical AND: If the first condition is false, the subsequent condition does not need to be executed.
Represented in code: if($x && $y) If $x is already false, there is no need to execute the subsequent $y.
Logical OR: If the first condition is true, there is no need to execute it later.
Represented in code: if($x || $y) If $x is already true, there is no need to execute the subsequent $y.
Let’s write a piece of code to prove it:
The code is as follows, try changing two ampersands into one ampersand:
Let’s take a look at the logical OR of the short circuit:
Change it to a | and look at the execution result
Through the above example, we know the difference between && and &, and the difference between || and | . We also learned what a short circuit is. So where can we use short circuit? There are some strange writing methods that we must understand clearly. In fact, it is the re-application of basic grammar.
Review the last paragraph of 3.3.1:
The above code is the code of a typical short-circuit application
exit means to stop running here and exit. . The following PHP code is no longer executed. It has two usages:
1, direct exit; that is, exit directly
2, exit('prompt content'), a prompt content is also given when exiting
exit
pronunciation :[ˈeksɪt]
Explanation: Exit