当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > PHP If...Else
The if, elseif and else statements in PHP are used to perform different actions based on different conditions.
PHP中的“if、elseif和else”的语句(即:条件语句),它是作用是根据不同的条件,执行不同的语句。
Very often when you write code, you want to perform different actions for different decisions.
你在书写代码是经常会使用条件语句:
You can use conditional statements in your code to do this.
你可以在代码中可以使用的条件语句及功能如下:
If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.
如果你希望在条件为真(true)或为假(false)时执行某段代码,你可以使用if....else语句;
if (条件) |
The following example will output "Have a nice weekend!" if the current day is Friday, otherwise it will output "Have a nice day!":
如果今天是星期五,下面这个例子将输出“Have a nice weekend!”;否则,它将输出“Have a nice day!”:
<html> <?php </body> |
If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:
在某一个条件(条件为真true/假false)的情况下,如果不止一条代码需要被执行,那么可以用大括号“{}”把它包含在内:
<html> <?php </body> |
If you want to execute some code if one of several conditions are true use the elseif statement
如果需要假设的条件不止一个时,可以使用elseif语句。
if (条件1) |
The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":
如果今天是“星期五”,下面的例子将输出“Have a nice weekend!”;如果今天是“星期日”,下面的例子将输出“Have a nice Sunday!”;如果是其它情况则输出“Have a nice day!”:
<html> <?php </body> |