PHP Switch

Luke

Madly Diligent
Joined
Jun 11, 2010
Messages
5,396
Reaction score
2
FP$
14
So I was wondering for you PHP people out there, who uses Switch?

Code:
function get_privacy_text($privacyoption){
    switch($privacyoption){
        case "0":
            return "all staff";
            break;
        case "1":
            return "invite only to view";
            break;
        case "2":
            return "system admins only";
            break;
        default:
            return "not set";
    }
}

this is an example of it. It is much cleaner than heaps of if statements, but typically when I talk to friends who do a fair bit of PHP programming they don't seem to even know it exists? Makes code a lot cleaner to read!
 
I use switch a lot. It's easier and cleaner than a ton of ifs and elseif's.
 
I now about the switch statement, but do not use it in any language for a few reasons.

A: it can potentially confuse people who are not familiar with the syntax.

B: it required two levels of indention, as opposed to an if statement, which requires one.

C: in most languages it is integer only. Not the case for PHP, but it is the case in Java.

D: It's more difficult to customize.

E: odd things happen when you omit a break;. The syntax seems a bit too close to goto statements in some ways, which is bad practice.
 
Most languages have switch, but I've never personally used it unless I'm taking a course that requires me to use it.
 
Yeah, I've never worked with anyone who didn't use them when they were available. They exist because they tend to make code more readable.

odd things happen when you omit a break;. The syntax seems a bit too close to goto statements in some ways, which is bad practice.
This isn't really odd behavior. It runs into the next case, which mostly is used when you want two cases to do the same thing. So, for example, in a Windows DLL entry point, you could do this:

Code:
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) 
{
    switch( fdwReason ) 
    { 
        case DLL_PROCESS_ATTACH:
        case DLL_THREAD_ATTACH:
            AttachBehavior(fdwReason);
            break;
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            DetachBehavior(fdwReason);
            break;
    }
    return TRUE;
}
The reason gotos are problematic is not because of their syntax, but because of how they can create "spaghetti code".
 
Cosmic said:
C: in most languages it is integer only. Not the case for PHP, but it is the case in Java.

As of version 1.7 Java supports String in switch statement.
 
kavin said:
Cosmic said:
C: in most languages it is integer only. Not the case for PHP, but it is the case in Java.

As of version 1.7 Java supports String in switch statement.
Oh, thanks for letting me know! 😀 That's pretty interesting.

Most of the Java code I write is for Java 1.6, unfortunately. At my day job (which is where I write the most java) we have to deal with a lot of outdated JREs, on systems which have really strict IT policies, so updating is probably an option, but would be a real pain. So, we mostly stick to 1.6. I'm glad to see that Java's improving in that regard, though! 😀
 
Back
Top Bottom