Home>Article>Backend Development> How to use if function in php

How to use if function in php

angryTom
angryTom Original
2019-08-22 15:19:29 7277browse

How to use if function in php

The if statement is a conditional statement. Used to perform different actions based on different conditions. Next we will introduce to you the usage of if function in PHP.

Recommended tutorial:PHP video tutorial

##PHP conditional statement

When you write code, you often want to perform different actions for different decisions. You can do this using conditional statements in your code.

In PHP, we can use the following conditional statements:

 ●if statement - If the specified condition is true, the code will be executed

 ●if...else statement - If the condition is true, execute the code; if the condition is false, execute the other end of the code

●if...elseif....else statement - execute different code blocks switch based on two or more conditions Statement - Select one of multiple blocks of code to execute

PHP - if statement

The if statement is used to specify a condition when

Execute code when true.

Syntax

if (条件) { 当条件为 true 时执行的代码; }

The following example will output "Have a good day!" if the current time (HOUR) is less than 20:

Example

PHP - if...else statement

Please use the if....else statement to execute the code when the condition is true, and to execute another section when the condition is false code.

Syntax

if (条件) { 条件为 true 时执行的代码; } else { 条件为 false 时执行的代码; }

If the current time (HOUR) is less than 20, the following example will output "Have a good day!", otherwise it will output "Have a good night!" :

Example

PHP - if...elseif....else statement

Please Use if....elseif...else statements to execute different code based on more than two conditions.

Syntax

if (条件) { 条件为 true 时执行的代码; } elseif (condition) { 条件为 true 时执行的代码; } else { 条件为 false 时执行的代码; }

If the current time (HOUR) is less than 10, the following example will output "Have a good morning!", if the current time is less than 20, then output "Have a good day!" . Otherwise "Have a good night!" will be output:

Example

The above is the detailed content of How to use if function in php. For more information, please follow other related articles on the PHP Chinese website!

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