根据我在手册中阅读的内容,我无法理解为什么我的代码返回if语句的else部分。如果日期('D')返回 Tue ,为什么不运行if部分?
<?php
date_default_timezone_set('UTC');
$weekdays = array("Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun");
$today = date('D');
if(in_array("Mon", "Tue", "Wed", "Thur", "Fri", $weekdays) == $today)
{
echo "It's" . " ";
echo $today;
echo " " . "Get out of bed and go to work";
}else{
echo "Do whatever you want becuase it's" . " ";
echo $today;
};
?>
我尝试过各种各样的事情并将 if 部分更改为此,但无济于事。
if(in_array(array("Mon", "Tue", "Wed", "Thur", "Fri"), $weekdays) == $today)
有人能告诉我语法有什么问题吗?
答案 0 :(得分:5)
if(in_array($today, $weekdays))
{
echo "It's" . " ";
echo $today;
echo " " . "Get out of bed and go to work";
}else{
echo "Do whatever you want becuase it's" . " ";
echo $current_day;
};
in_array手册
答案 1 :(得分:2)
in_array
函数checks if a value exists in an array, returning true or false。
如果您想知道$today
是否是工作日,您可能希望执行以下操作:
$weekdays = array("Mon", "Tue", "Wed", "Thur", "Fri");
if(in_array($today, $weekdays)) {
...
}