简体   繁体   中英

PHP Switch-Case not working with certain string?

I'm trying to get switch/case to work with some variables and they aren't working and I am wondering why:

function convert_time($time_code) {
    switch ($time_code) {
        case "8:00a-10:00p":
            return 1;
            break;
    }
}

Then the code that calls this function is:

$testvariable = "8:00a-10:00p";
$testtimecode = covert_time($testvariable);
echo "TTC: $testtimecode";

It always outputs "TTC:"

I went to PhpFiddle and tested it and it also doesn't work there, but I couldn't find a way to make a link to it like in jsfiddle.

However, if I do this code:

$time_code = "8:00a-10:00a";
if ($time_code == "8:00a-10:00a") {echo "yes";} else {echo "no";}

It will echo yes.

So my question is, what about the format of my 8:00a-10:00a is breaking the switch? and is it fixable.

Nevermind. I found my problem and it was a typo.

It should have been 8:00a-10:00a, and it was 8:00a-10:00p.

sorry!

Got this to work on my local server:

function convert_time($time_code) {
    switch ($time_code) {
        case "8:00a-10:00p": return 1;
    }
}

$testvariable = "8:00a-10:00p";
$testtimecode = convert_time($testvariable);
echo "TTC: $testtimecode";

not quite sure what could've happened on your end, possibly something wrong with your server itself but give this a shot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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