php switch

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sefs
Forum Newbie
Posts: 3
Joined: Thu Mar 26, 2009 3:12 pm

php switch

Post 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?
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: php switch

Post 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?
sefs
Forum Newbie
Posts: 3
Joined: Thu Mar 26, 2009 3:12 pm

Re: php switch

Post 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.
sefs
Forum Newbie
Posts: 3
Joined: Thu Mar 26, 2009 3:12 pm

Re: php switch

Post by sefs »

Disregard that last post...your way works. I was uploading the updated file with your suggestion to an incorrect location.

Thanks :)
Post Reply