简体   繁体   English

在if / else中使用in_array和date的问题

[英]Problems using in_array and date in if/else

I am experimenting with the date and in_array functions of PHP. 我正在试验PHP的datein_array函数。

Based on what I have read in the manual I can't understand why my code is returning the else part of the if statement. 基于我在手册中读到的内容,我无法理解为什么我的代码返回if语句的else部分。 If the date('D') is returning Tue why doesn't it run the if part? 如果date('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;
};
?>

I've tried various things and and changed the if part to this, but to no avail. 我尝试了各种方法,并将if部分更改为此,但无济于事。

if(in_array(array("Mon", "Tue", "Wed", "Thur", "Fri"), $weekdays) == $today)

Can someone tell me what is wrong with the syntax? 有人能告诉我语法有什么问题吗?

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 manual in_array手册

The in_array function checks if a value exists in an array, returning true or false . in_array函数检查数组中是否存在值,返回true或false

If you want to know if $today is a weekday, you would want to do something like: 如果您想知道$today是否是工作日,则可以执行以下操作:

$weekdays = array("Mon", "Tue", "Wed", "Thur", "Fri");
if(in_array($today, $weekdays)) {
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM