Page 1 of 1

php switch

Posted: Thu Mar 26, 2009 3:16 pm
by sefs
Hi all,

I am having a problem constructing a php swtich statement that looks like this

Code: Select all

 
switch ($HostURL) { // Find which site to redirect to
 
    Case ("a") || ("b"):
                echo "1";
            break;
    Case ("c") || ("d"):
                echo "2";
        break;
    default:  // Default to main page
              echo "3";
}
 
But something is wrong with the above as it keeps defaulting to the first case statement all the time no matter the value of hosturl. The only way i can make it work as desired is if i converted it to an if else statement. Is there a way to make it work for the switch?

Re: php switch

Posted: Thu Mar 26, 2009 3:30 pm
by greyhoundcode
Try the following:

Code: Select all

$HostURL = "e";
 
switch ($HostURL) 
{
    Case "a":
    case "b":
        echo "1";
        break;
        
    Case "c":
    case "d":
        echo "2";
        break;
        
    default:
        echo "3";
}
 
// Outputs:
// 3
Note that I have disposed of the OR symbol and am instead using fall throughs. Hopefully that helps?

Re: php switch

Posted: Thu Mar 26, 2009 3:42 pm
by sefs
greyhoundcode wrote:Try the following:

Code: Select all

$HostURL = "e";
 
switch ($HostURL) 
{
    Case "a":
    case "b":
        echo "1";
        break;
        
    Case "c":
    case "d":
        echo "2";
        break;
        
    default:
        echo "3";
}
 
// Outputs:
// 3
Note that I have disposed of the OR symbol and am instead using fall throughs. Hopefully that helps?
I am baffled ... it always runs the first case in this one too.

Re: php switch

Posted: Thu Mar 26, 2009 3:55 pm
by sefs
Disregard that last post...your way works. I was uploading the updated file with your suggestion to an incorrect location.

Thanks :)