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?
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.
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.
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.
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.
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
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;
}
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With