嵌套If函数中的语句

时间:2015-12-29 20:32:23

标签: php if-statement

嗨所以我试图制作一个实际检测时间并取决于日期(H)的功能;显示现在发生的课程和其他一些信息。

$holidays = false;
//$hour = date('H');
 $hour = "11"; //Changed it to test the code

function classes()
{
    if ($holdays == false) //Are we on holidays? No
    {
        if($hour > "9" && $hour < "21") //University working hours? yes
        {   
            if ($hour = "10") //Hour 10? classes below
            { 
                echo "<tr>";
                    echo '<th class="course-title"><a href="course.html">...</a></th>';
                    echo '<th class="course-category"><a href="">...</a></th>';
                    echo "<th>...</th>";
                    echo "<th>...</th>";
                    echo "<th>...</th>";
                echo "</tr>";
                //Another Class
                echo "<tr>";
                    echo '<th class="course-title"><a href="course.html">...</a></th>';
                    echo '<th class="course-category"><a href="">...</a></th>';
                    echo "<th>...</th>";
                    echo "<th>...</th>";
                    echo "<th>...</th>";
                echo "</tr>";
               //Another Class
                echo "<tr>";
                    echo '<th class="course-title"><a href="course.html">...</a></th>';
                    echo '<th class="course-category"><a href="">...</a></th>';
                    echo "<th>...</th>";
                    echo "<th>...</th>";
                    echo "<th>...</th>";
                echo "</tr>";
            }
            else{ //Hour not 10?
                echo "No classes right now.";
            }
        }
        else{ //Not working hours?
            echo "University closed at this time.</br>";

        }
    }
    else{ //Vacation?
            echo "University closed for vacation!";
    } 

}

所以我将$ hour值设置为11因此它将成功传递前两个ifs并且我希望它会回显现在没有类,但它返回大学此时关闭。我也可能搞砸代码上的}。

PS:我知道我正在使用许多回声我不是那么有经验但仍然没有用字符串

1 个答案:

答案 0 :(得分:1)

请注意您的变量范围。要在函数内部访问$ holidays,请使用全局范围声明它。

$holidays = false;
$hour = "11"; //Changed it to test the code

function classes()
{
    global $holidays, $hour;   // <=== HERE
    if ($holidays == false) //Are we on holidays? No
    {
      // your code here
    {
}

此外,您还有其他人在您的代码中指出的错误。

  

if($ hour =“10”)if if($ hour ==“10”) - Fabio

  

$ holidays!= $ holdays your missing a i - cmorrissey