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 executePHP - if statement
The if statement is used to specify a condition whenExecute code when true.
Syntaxif (条件) { 当条件为 true 时执行的代码; }
<?php $t=date("H"); if ($t<"20") { echo "Have a good day!"; } ?>
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 时执行的代码; }
<?php $t=date("H"); if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
PHP - if...elseif....else statement
if (条件) { 条件为 true 时执行的代码; } elseif (condition) { 条件为 true 时执行的代码; } else { 条件为 false 时执行的代码; }
<?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!"; } ?>
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!