so I have a function that handles various other functions, and I use a switchstatement to handle these functions with cases and breaks.
It all works fine. Would it be possible for me to change this into an array with keys?
Here's the code
switch ($intMultiFun) {
case "a":
handle a function
break;
case "b":
handle a function
break;
case "c":
handle a function
break;
case "d":
handle a function
break;
}
$map = array(
'a' => 'a_func_name',
'b' => 'b_func_handler_name',
...
);
if (array_key_exists($intMultiFun, $map)) {
call_user_func($map[$intMultiFun]); // optionally you can pass parameters too
}
You're looking for in_array(), it seems:
if(in_array($intMultiFun, ['a', 'b', 'c', 'd']))
{
//handle a function
}
-short arrays syntax was introduced in PHP>=5.4, so in lower versions that will be
if(in_array($intMultiFun, array('a', 'b', 'c', 'd')))
{
//handle a function
}
Edit
If functions would be different, then you should hold them in array, like:
$rgFunctions = ['a'=>'funcA', 'b'=>'funcB', 'c'=>'funcC', 'd'=>'funcD'];
if(array_key_exists($intMultiFun, $rgFunctions) &&
function_exists($rgFunctions[$intMultiFun]))
{
$mResult = call_user_func($rgFunctions[$intMultiFun]);
}
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