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.

ExampleExplanationDetailed explanation
$ x and $yLogical AND (and relationship) Returns true if $x and $y are true
$x && $y Same as above Same as above
$x or $yLogical 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
!$xLogical notInversion, that is, true becomes false, false becomes true
$x xor $y Logical exclusive ORIf 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:

<?php

$x = true;
$y = false;
//逻辑与(并且),要求两个都为true才执行真区间,所以代码中执行假区间
if($x && $y){
   echo '执行了真区间';
}else{
   echo '执行了假区间';
}
?>

Logical OR:

<?php

$foo = false;
$bar = true;
//逻辑或,有一个为真则为真
if($foo || $bar){
   echo '执行真区间';
}else{
   echo '执行假区间';
}

?>

Logical NOT:

<?php

$foo = false;

//逻辑非,把false变为了true
if(!$foo){
   echo '执行真区间';
}else{
   echo '执行假区间';
}

?>

【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:

<?php

$x = false;
$y = 2;
if($x && $y++){
   echo '真';
}else{
   echo '假';
}
//结果还为2,说明没有执行$y++
echo $y;
?>

The code is as follows, try changing two ampersands into one ampersand:

<?php

$x = false;
$y = 2;
if($x & $y++){
   echo '真';
}else{
   echo '假';
}
//再看看结果
echo $y;
?>

Let’s take a look at the logical OR of the short circuit:

<?php

$x = true;
$y = 2;
if($x || $y++){
   echo '真';
}else{
   echo '假';
}
//结果,因为$x已经为true了,肯定执行真区间没有必要执行$y++了
echo $y;
?>

Change it to a | and look at the execution result

<?php

$x = true;
$y = 2;
if($x | $y++){
   echo '真';
}else{
   echo '假';
}
//自己运行对比结果
echo $y;
?>

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:

<?php
//如果为defined('AUTH')存在AUTH常量则为true,不访问后面的exit了。如果为false则执行exit
defined('AUTH') or exit('存在安全因素不准访问');
?>

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

Continuing Learning
||
<?php $x = true; $y = 2; if($x | $y++){ echo '真'; }else{ echo '假'; } //自己运行对比结果 echo $y; ?>
submitReset Code