Home > Article > Backend Development > How to abbreviate IF judgment in PHP
This article mainly introduces how to abbreviate IF judgment in PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
The first type: IF conditional statement
Second type: ternary operation
Third type: conditional statement composed of && and ||
First type: IF Needless to say, this is the basis, I believe it Most people will;
Second: c=a>b? true:false //Means: If a>b is true, return true, otherwise return false (of course it can be replaced with a statement) , And return the result to C;
3:
1, &&
## In most languages, he means and of Both sides are true, using PHP as an example, it is used in the tradition;if ($a>0 && $b>0){ //语句; }Execute the statement when both are true; However, today we will use it as a conditional statement; For example, there is a traditional conditional statement below:
if ($a>0){ $b='This is test'; }When the condition is true, the statement in it will be executed; but it is too troublesome to write this way, we can write it directly like this: $a>0 && ($b='This is test'); The computer will first determine whether $a is true. If so, the following statements will be executed. If not, there is no need to execute the following statements;Benefits: 1. It can be written in one line, 2. Omit the code; || Of course it is possible, but the order of execution is different. $a>0 || ($b='This is test'); No: Execute; The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website! Related recommendations:
Explanation of string functions in PHP
Introduction
How to solve the problem of php in Array reference problem left after foreach loop
The above is the detailed content of How to abbreviate IF judgment in PHP. For more information, please follow other related articles on the PHP Chinese website!