这是代码:
function change_case($str, $type) {
return str'.$type.'($str);
}
change_case('String', 'tolower');
Run Code Online (Sandbox Code Playgroud)
它返回一个解析错误.我究竟做错了什么?
要使用变量函数,可以先构建函数名并将其放在变量中,然后调用它(function_exists()如果有人传递了无效类型,请使用):
function change_case($str, $type) {
$func = 'str' . $type;
if (function_exists($func))
return $func($str);
else
return $str;
}
Run Code Online (Sandbox Code Playgroud)
不知道你为什么要写这样的功能strtolower(),strtoupper()尽管如此.即使你想自定义函数来涵盖lower和upper,变量函数的调用是不必要的.
| 归档时间: |
|
| 查看次数: |
49 次 |
| 最近记录: |