How to use if function in php

angryTom
Release: 2023-04-07 09:10:02
Original
7323 people have browsed it

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 时执行的代码;
}
Copy after login

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

Example

<?php
    $t=date("H");
    if ($t<"20") {
      echo "Have a good day!";
    }
?>
Copy after login

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 时执行的代码;
}
Copy after login

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
    $t=date("H");
    if ($t<"20") {
      echo "Have a good day!";
    } else {
          echo "Have a good night!";
    }
?>
Copy after login

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 时执行的代码;
}
Copy after login

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

<?php
    $t=date("H");
    if ($t<"10") {
      echo "Have a good morning!";
    } elseif ($t<"20") {
      echo "Have a good day!";
    } else {
      echo "Have a good night!";
    }
?>
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!