spechter
spechter

Reputation: 13

What is a more ressource using way to select basing on a variables value?

Hello Stackoverflow community,

i whonder if there is an less ressource using way to select an action based on a variables value than having multiple if-clauses. I think there was something like

select $variable
case value1 {some code}
case value2 {other code}

but is it more lightweight than several if clauses ?

Thanks in advance, spechter

Upvotes: 0

Views: 28

Answers (1)

wavemode
wavemode

Reputation: 2116

Yes, php, like many C-flavored languages, has the switch statement to avoid using many if statements:

switch($testVar) {
    case 1:
        handleOne();
        break;
    case 2:
        handleTwo();
        break;
    case 3:
        handleThree();
        break;
    default:
        handleOther();
}

Upvotes: 1

Related Questions