Home > Backend Development > PHP Tutorial > How to abbreviate IF judgment in PHP

How to abbreviate IF judgment in PHP

不言
Release: 2023-04-02 22:54:01
Original
5203 people have browsed it

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){   
     //语句;   
}
Copy after login

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';
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template