语法 if (condition) code to be executed if condition is true; else code to be executed if condition is false;
实例 如果当前日期是周五,下面的代码将输出 “Have a nice weekend!”,否则会输出 “Have a nice day!”:
代码如下
<html> <body>
<?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?>
</body> </html>
如果需要在条件成立或不成立时执行多行代码,应该把这些代码行包括在花括号中:
代码如下
<html> <body>
<?php $d=date("D"); if ($d=="Fri") { echo "Hello!<br />"; echo "Have a nice weekend!"; echo "See you on Monday!"; } ?>
</body> </html>
ElseIf 语句 如果希望在多个条件之一成立时执行代码,请使用 elseif 语句:
语法 if (condition) code to be executed if condition is true; elseif (condition) code to be executed if condition is true; else code to be executed if condition is false;
实例 如果当前日期是周五,下面的例子会输出 “Have a nice weekend!”,如果是周日,则输出 “Have a nice Sunday!”,否则输出 “Have a nice day!”:
代码如下
<html> <body>
<?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!"; ?>