我需要一些幫助和解釋如何簡化我的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';
}
}
?>
Regards R.C.
的可能的複製[簡化PHP代碼](https://stackoverflow.com/questions/6779163/simplifying-php-code) – Jacob
如果這是工作的代碼,我會說這應該在[CodeReview](https://codereview.stackexchange.com/)討論,而不是Stack Overflow。 – agrm