Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using variables for the cases and a function for a switch?

In PHP I'd like to do this:

switch (function_foo($bar,$bar2)) {
case $fu:
    *Do Stuff*
    break;
case $fubar:
    *Do Other Stuff*
    break;
}

Is this a terrible idea? Will it work?

like image 948
aslum Avatar asked Mar 04 '10 21:03

aslum


People also ask

How do you use variables in a switch case?

Some Important Rules for Switch Statements The value for a case must be of the same data type as the variable in the switch. The value for a case must be constant or literal. Variables are not allowed. The break statement is used inside the switch to terminate a statement sequence.

Can a variable expression be used in switch case?

The expression in the switch can be a variable or an expression - but it must be an integer or a character. You can have any number of cases however there should not be any duplicates. Switch statements can also be nested within each other. The optional default case is executed when none of the cases above match.

What variables are used in a switch statement?

Syntax. The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums. You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

Why do we use the variable inside the switch statement?

If a programmer declares variables, initializes them before the first case statement, and then tries to use them inside any of the case statements, those variables will have scope inside the switch block but will not be initialized and will consequently contain indeterminate values.


4 Answers

Using a function in the switch is OK : the function will be called, and will return a value -- which is the one that will be used for the case.

It's exactly the same as writing :

$my_var = function_foo($bar,$bar2);
switch ($my_var) {
    // ...
}

Even if I prefer using a variable, so the code is easier to read.


And using variables in the case is something you don't see often ; but it works fine too ;-)

Quoting the manual page of switch :

The case expression may be any expression that evaluates to a simple type, that is, integer or floating-point numbers and strings.

So, your code will work, as long as $fu and $fubar contain simple-type values.


Using a variable as a case value not often done (as far as I can tell from the code I read), probably because some other languages don't allow that (for instance, C doesn't allow that ; and the switch/case structure is borrowed from C) ; but it works :

$a = 1;
$b = 2;
switch (1) {
    case $a: echo 'a'; break;
    case $b: echo 'b'; break;
}

Will output :

a
like image 131
Pascal MARTIN Avatar answered Oct 23 '22 15:10

Pascal MARTIN


Never tried a function as parameter to the switch, not sure (You should give it a try), however you can first store function return value in some variable and use that in switch eg:

$return_value = function_foo($bar, $bar2);

switch ($return_value) {
case $fu:
    *Do Stuff*
    break;
case $fubar:
    *Do Other Stuff*
    break;
}
like image 34
Sarfraz Avatar answered Oct 23 '22 16:10

Sarfraz


According to the manual, a PHP switch statement is exactly like a series of if/else if statements (if every case ends with break). That means your technique should work. As long as the function names and variable names are readable, I can't think of any problems with it.

In some other languages, the switch statement is actually a performance improvement over if/else if statements, so you need to know the case values at compile time. It doesn't look like PHP does that kind of thing.

like image 28
Don Kirkby Avatar answered Oct 23 '22 17:10

Don Kirkby


Its posibble yes and its called lambda, which are hidden functions

$lambda = function($a, $b) {
    return $a * $b;
};


$return_value = function foo($bar, $bar2){ return $logic }

switch ($lambda(2,4)) {
case $fu:
    *Do Stuff*
    break;
case $fubar:
    *Do Other Stuff*
    break;
}
like image 42
streetparade Avatar answered Oct 23 '22 16:10

streetparade