When we develop PHP pages, sometimes we need to read information from the database or page according to different conditions, and then execute the corresponding statements. When it comes to needs in this area, we need to understand and master the use of some conditional statements in PHP. This article will give you a detailed introduction to PHP's if conditional judgment statement, if...else conditional statement and if...elseif ...elseSpecific usage of multiple conditional statements.
Below we will give you a detailed explanation through specific code examples.
1. PHP if conditional judgment statement
Code example:
<?php $t=date("H"); if ($t<"20") { echo "当前时间小于20时,输出此段文字"; } ?>
Access through the browser, the judgment result is as follows:
We can understand the above code in this way. First, use date to obtain the current time, and then use the if statement to determine whether the current time is less than 20. If it is less than 20, echo will output the specified statement. As shown in the figure, the specified text is successfully output. (Recommended to refer to the online course: [Learn PHP Programming at Your Fingertips] Chapter 3 - PHP Conditional Statements)
## 2. PHP if...else conditional judgment statement
The code example is as follows:<?php $t=date("H"); if ($t<"20") { echo "if判断如果当前时间小于20则输出此段文字"; } else { echo "否则输出这段文字"; } ?>
3. PHP if...elseif....else multi-condition judgment statement
The code example is as follows:<?php $t=date("H"); if ($t<"10") { echo "早上好"; } elseif ($t<"20") { echo "美好的一天"; } else { echo "晚上好"; } ?>
The above is the detailed content of How to understand and use if-related conditional statements in PHP?. For more information, please follow other related articles on the PHP Chinese website!