控制构造(Control Structures)


if 语句

if 语句是根据定义的表现形式的结果,决定是要执行指定的命令行或是跳过的最基本的控制构造中的一个。

<?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!
  • 递归if 语句使用例
<?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
    }
?>