猿问

PHP switch case,0 没有被识别为一个值?

我在 PHP 脚本中有一个 switch case 语句,它根据 JS 文件中的 Ajax 调用提供的“日”和“小时”值返回一个字符串值。但是只要这些值为 0(即,星期日为日变量)和 0 作为小时变量的午夜)该语句不起作用并且该值返回空。我似乎找不到解决方案,这里是 PHP 函数和 Ajax 调用的代码的较短版本;


function getDayTimeData($day, $time){

    $program = "";

    switch($day){

        case 0: //Sunday

            switch($time){

                case $time < 1:

                    $program = 'Be Our Guest';

                    break;

                case $time < 6:

                    $program = 'Club Technoise';

                    break;

                case $time < 12:

                    $program = 'Morning Juice';

                    break;

                case $time < 21:

                    $program = 'Detox';

                    break;

                case $time < 24:

                    $program = 'Disco Ball';

                    break;

            }

            break;

     }

}

if(isset($_POST["DayData"]) && isset($_POST["TimeData"]))

{

    echo(getDayTimeData($_POST["DayData"],$_POST["TimeData"]));

}

这是 Jquery Ajax 脚本


function getDayTimeAjax(_day,_time){

    var def2 = $.Deferred();

    

    setTimeout(function(){

        if(xhr && xhr.readyState != 4){

            xhr.abort();

        }

        var xhr = $.ajax({

            type: 'post',

            url: 'returnPoster.php',

            contentType: "application/x-www-form-urlencoded; charset=UTF-8",

            data: {DayData: _day, TimeData: _time},

            dataType: 'text',

            success: function(x){

                programInfo = x;

                def2.resolve();

            }

        })

    },2000);

    return $.when(def2).done(function(){}).promise();

}


var d = new Date();

var programInfo = ".";


setInterval(function () { updateProgram() }, 3000);


function updateProgram(){

    getDayTimeAjax(d.getDay(),d.getHours()).done(function(){

        console.log(programInfo.trim());

    })

}

编辑:抱歉,大家看起来我在这个主题上提供了一些错误信息,因为当我设置这个线程时我有点着急。该问题仅在 $time 值为 0 时发生。这些值看起来很好,直到发生“case $time <1:”操作。从那时起,即使 $time 变量的值为 0,它也不会执行第一种情况下的代码块,因此程序变量保持为空。所有其他值都返回 switch 语句中的值。


慕虎7371278
浏览 159回答 4
4回答

梵蒂冈之花

这是一个小例子。这将回显基于时间的程序。为了确定日期,我建议使用 IF 而不是 switch case。$time = date("H");$day = date("w");$program = "";if($day == 0){&nbsp; switch($time){&nbsp; &nbsp; case $time < 1:&nbsp; &nbsp; &nbsp; $program = 'Be Our Guest';&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case $time < 6:&nbsp; &nbsp; &nbsp; $program = 'Club Technoise';&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case $time < 12:&nbsp; &nbsp; &nbsp; $program = 'Morning Juice';&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case $time < 21:&nbsp; &nbsp; &nbsp; $program = 'Detox';&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case $time < 24:&nbsp; &nbsp; &nbsp; $program = 'Disco Ball';&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; }else{&nbsp; &nbsp; &nbsp;.......&nbsp; }echo $program;

沧海一幻觉

你不能像这样比较整数值和时间,switch($time){&nbsp; &nbsp; case $time < 1:&nbsp; &nbsp; &nbsp; break;}尝试比较相同类型的值。

慕田峪7331174

你的问题出在 switch 语句上,如果你这样做:&nbsp; &nbsp; switch($time){&nbsp; &nbsp; &nbsp; &nbsp; case $time < 1:&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }并为 $time 填写一个值,例如 0:&nbsp; &nbsp; switch(0){&nbsp; &nbsp; &nbsp; &nbsp; case 0 < 1:&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }然后将调用中的值switch()与之后的值进行比较case:0 == true嗯,这个——以及所有其他比较——都是false。如果您确实想在 switch 语句中使用 less-than 进行比较,则需要执行以下操作:&nbsp; &nbsp; switch(true){&nbsp; &nbsp; &nbsp; &nbsp; case $time < 1:&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }那么比较结果就是:true == true请注意,在您的原始代码中,当 的 值$time不为 0 时,它会比较 is 3 == true,因为switch没有严格检查(它使用 ==,而不是 ===)true。无论值如何,它总是返回第一个结果“Be our guest”。

胡说叔叔

我认为你可以尝试这个,它也可能适用于第一个开关,但我更喜欢使用if:function getDayTimeData ($day, $time){&nbsp; &nbsp; if ((int) $day === 0) {&nbsp; &nbsp; &nbsp; &nbsp; switch($time){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case $time < 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $program = 'Be Our Guest';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case $time < 6:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $program = 'Club Technoise';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case $time < 12:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $program = 'Morning Juice';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case $time < 21:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $program = 'Detox';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case $time < 24:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $program = 'Disco Ball';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}if(isset($_POST["DayData"]) && isset($_POST["TimeData"])){&nbsp; &nbsp; echo(getDayTimeData($_POST["DayData"],$_POST["TimeData"]));}
随时随地看视频慕课网APP
我要回答