1

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.

3
  • If that code is verbatim you have a typo on your function call, should be convert_time($testvariable); (missing n).
    – cOle2
    Commented Nov 6, 2014 at 21:35
  • Works fine here. But note you've got a typo in the above code: covert != convert.
    – Marc B
    Commented Nov 6, 2014 at 21:36
  • That was a typo on this page, not my code but thank you. I found my problem and it was another typo. I even triple checked before I posted on here it wasn't that but i guess the eyes want to see what they want to see. Commented Nov 6, 2014 at 21:37

2 Answers 2

1

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!

1
  • @MarcB -10:00a -> -10:00p Commented Nov 6, 2014 at 21:39
1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.