Home  >  Article  >  Backend Development  >  Conditional statements written with && php - three types of conditional statements

Conditional statements written with && php - three types of conditional statements

WBOY
WBOYOriginal
2016-08-08 09:19:561217browse

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 most of them Everyone knows it;

Second type: 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 ;

Third type: 1, &&

In most languages, he means and meaning, that is, the left and right sides are true, using PHP as an example, traditional use in tradition;

if ($a>0 && $b>0){ 
     //语句; 
}

Execute the statement when both are true;

However, today we are going to 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 statements in it will be executed; but it is too troublesome to write like this. We can write 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. Can be written in one line

2, omitting code;

2, ||

&& can write conditional statements like this, then || Of course, it is also possible, but his execution order is different.

$a>0 || ($b='This is test');

The computer first determines whether $a >0 is true, yes: the following statements will not be executed, no: they will be executed;



Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the three conditional statements written in && PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn