1

我目前正在编写一些似乎在逻辑上工作的 PHP 代码

$tod = date("G");
if($tod >= 5 && $tod <= 11)      { $work = "Morning's"; }
elseif($tod >= 12 && $tod <= 17) { $work = "Afternoon's"; }
else                               $work = "Evening's";

$dw = date( "l");

if($work = "Morning's")       { $pre = "Morning"; }
elseif($work = "Afternoon's") { $pre = "Afternoon"; }
else                            $pre = "Evening";

出于某种原因,如果我在回显 $work 时尝试将 $tod 变量增加或设置为每个范围内的某个值,无论 $tod 设置为什么,它总是返回“Morning's”。我的 if..elseif..else 语句有问题吗?加载页面时,我没有收到任何错误。

谢谢您的帮助

4

2 回答 2

0

试试这个,如果你忘记了两个“=”

  $tod = date("G");
    if($tod >= 5 && $tod <= 11) {$work = "Morning's";}
    elseif($tod >= 12 && $tod <= 17) {$work = "Afternoon's";}
    else $work = "Evening's";
    $dw = date( "l");
    if($work == "Morning's"){$pre = "Morning";}
    elseif($work == "Afternoon's"){$pre = "Afternoon";}
    else $pre = "Evening";
于 2012-09-27T15:42:23.910 回答
0

只是为了缩短一点(并更正拼写:复数是 w/o '):

$pre = "Evening";
in_array( date( "G" ), range( 5, 11 ) )  AND $pre  = "Morning";
in_array( date( "G" ), range( 12, 17 ) ) AND $pre = "Afternoon";
$work = "{$pre}s";
于 2012-09-27T16:21:58.867 回答