Here is the code:
function change_case($str, $type) {
return str'.$type.'($str);
}
change_case('String', 'tolower');
It returns a parse error. What am I doing wrong?
To use a variable function, you build the function name and put it in a variable first, then call it like so (use function_exists() in case someone passes an invalid type):
function change_case($str, $type) {
$func = 'str' . $type;
if (function_exists($func))
return $func($str);
else
return $str;
}
No idea why you'd want to write such a function for strtolower() and strtoupper() though. Even if you wanted a custom function to cover both lower and upper, a variable function call is unnecessary.
Why are you creating a function to call a single built-in PHP function? This seems completely backwards and will never, ever be worth the trouble. You can fix your problem by using the built-in PHP functions strtolower or strtoupper.
$, and.is used for concatenation.