亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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才執(zhí)行真區(qū)間,所以代碼中執(zhí)行假區(qū)間
if($x && $y){
   echo '執(zhí)行了真區(qū)間';
}else{
   echo '執(zhí)行了假區(qū)間';
}
?>

Logical OR:

<?php

$foo = false;
$bar = true;
//邏輯或,有一個為真則為真
if($foo || $bar){
   echo '執(zhí)行真區(qū)間';
}else{
   echo '執(zhí)行假區(qū)間';
}

?>

Logical NOT:

<?php

$foo = false;

//邏輯非,把false變?yōu)榱藅rue
if(!$foo){
   echo '執(zhí)行真區(qū)間';
}else{
   echo '執(zhí)行假區(qū)間';
}

?>

【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 '假';
}
//結(jié)果還為2,說明沒有執(zhí)行$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 '假';
}
//再看看結(jié)果
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 '假';
}
//結(jié)果,因為$x已經(jīng)為true了,肯定執(zhí)行真區(qū)間沒有必要執(zhí)行$y++了
echo $y;
?>

Change it to a | and look at the execution result

<?php

$x = true;
$y = 2;
if($x | $y++){
   echo '真';
}else{
   echo '假';
}
//自己運行對比結(jié)果
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則執(zhí)行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 '假'; } //自己運行對比結(jié)果 echo $y; ?>
submitReset Code