Home >Backend Development >PHP Tutorial >PHP process control, php process_PHP tutorial
if statement is used to execute code only when the specified condition is true.
Grammar
if (条件) { 条件成立时要执行的代码; }
If the current time is less than 20, the following example will output "Have a good day!":
Example
<?php $t=date("H"); if ($t<"20") { echo "Have a good day!"; } ?>
Run
To execute a block of code when the condition is true, and to execute another block of code when the condition is not true, please use the if....else statement.
Grammar
if (条件) { 条件成立时执行的代码; } else { 条件不成立时执行的代码; }
If the current time is less than 20, the following example will output "Have a good day!", otherwise it will output "Have a good night!":
<?php $t=date("H"); if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
To execute a block of code when one of several conditions is true, use the if....else if...else statement. .
Grammar
if (条件) { if 条件成立时执行的代码; } else if (条件) { elseif 条件成立时执行的代码; } else { 条件不成立时执行的代码; }
If the current time is less than 10, the following example will output "Have a good morning!", if the current time is not less than 10 and less than 20, it will output "Have a good day!", otherwise it will output "Have a good night!" ":
<?php $t=date("H"); if ($t<"10") { echo "Have a good morning!"; } else if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
Original address: http://www.manongjc.com/php/php_if_switch.html
Related reading:
php date_sunrise() gets the sunrise time based on the location (latitude and longitude)
php global legal time zone list
php date_default_timezone_set() sets the default time zone for date/time functions
php date_default_timezone_get() gets the default time zone used by date and time functions
php checkdate() function verifies whether the date is legal (correct)
A must-have for climbing Mount Tai to watch the sunrise - Mount Tai’s sunrise time list