if 语句是根据定义的表现形式的结果,决定是要执行指定的命令行或是跳过的最基本的控制构造中的一个。
if 语句的构造
语法构造 | 说明 |
---|---|
if (表现形式) 命令语句; |
表现形式的结果为TRUE时,实行命令语句;表现形式为FALSE不实行命令语句。 |
<?php
$var1 = $var2 = 1;
if($var1 == $var2) // expression is TRUE
echo "var1 and var2 are equal"; // statement will be executed
?>
[result]
var1 and var2 are equal
<?php
$var1 = 1;
$var2 = 2;
if($var1 < $var2)
{ // grouping by curly braces
echo "var1 is smaller than var2";
echo "\r\nbye!";
} // grouping by curly braces
?>
[result]
var1 is smaller than var2
bye!
<?php
$var1 = $var2 = 1;
$var3 = 2;
if($var1 == $var2) // expression is TRUE
{
if($var1 < $var3) // expression is TRUE
echo "var1 and var2 are equal"; // statement will be executed
}
?>