DerekConlon
DerekConlon

Reputation: 463

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.

Upvotes: 1

Views: 886

Answers (2)

iam-decoder
iam-decoder

Reputation: 2564

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.

Upvotes: 1

DerekConlon
DerekConlon

Reputation: 463

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!

Upvotes: 1

Related Questions