如何简化这个PHP代码

时间:2017-10-21 13:04:17

标签: php

我需要一些帮助并解释如何简化我的PHP代码。

它正在运行,但似乎很少重复代码。

基本上我想查看商店关闭时显示预先格式化文本的日期和时间以及桌面设备或移动设备上的不同文本。

感谢阅读。

    <?php
    include 'Mobile_Detect.php';
    $detect = new Mobile_Detect();
    //Get time
    $hour = date("H", mktime(date("H")));
    $day = date("D");

    if($day == 'Mon'  && ($hour >= 6 && $hour < 22)) {
        if ($detect->isMobile()) 
        {
        include 'online_mobile.php';
        } else 
        {
        include 'online_desktop.php'; 
        }
    }       
    elseif($day == 'Tue'  && ($hour >= 6 && $hour < 22)) {
        if ($detect->isMobile()) 
        {
        include 'online_mobile.php';
        } else 
        {
        include 'online_desktop.php'; 
        }
    }                           
    elseif($day == 'Wed'  && ($hour >= 6 && $hour < 22)) {
        if ($detect->isMobile()) 
        {
        include 'online_mobile.php';
        } else 
        {
        include 'online_desktop.php'; 
        }
    }                   
    elseif($day == 'Thu'  && ($hour >= 6 && $hour < 22)) {
        if ($detect->isMobile()) 
        {
        include 'online_mobile.php';
        } else 
        {
        include 'online_desktop.php'; 
        }
    }                       
    elseif($day == 'Fri'  && ($hour >= 6 && $hour < 22)) {
        if ($detect->isMobile()) 
        {
        include 'online_mobile.php';
        } else 
        {
        include 'online_desktop.php'; 
        }
    }               
    elseif($day == 'Sat'  && ($hour >= 8 && $hour < 18)) {
        if ($detect->isMobile()) 
        {
        include 'online_mobile.php';
        } else 
        {
        include 'online_desktop.php'; 
        }
    }                       
    elseif($day == 'Sun'  && ($hour >= 8 && $hour < 18)) {
        if ($detect->isMobile()) 
        {
        include 'online_mobile.php';
        } else 
        {
        include 'online_desktop.php'; 
        }
} else {
    if ($detect->isMobile()) 
    {
    include 'closed_mobile.php';
    } else 
    {
    include 'closed_desktop.php'; 
    } 
}
?>

关心R.C。

1 个答案:

答案 0 :(得分:0)

尝试:

The final result was:

main.guitaristT{
    Surname:  "Hendrix",
    Year:     1942,
    American: true,
    Rating:   9.989999771118164,
    Styles:   {"blues", "rock", "psychedelic"},
    Cities:   {"London":11, "Bay Area":9, "New York":17, "Los Angeles":14},
}