PHP和我自己的函数中的奇怪错误

omt*_*mtr 0 php

这是代码:

function change_case($str, $type) {
    return str'.$type.'($str);
}
change_case('String', 'tolower');
Run Code Online (Sandbox Code Playgroud)

它返回一个解析错误.我究竟做错了什么?

Bol*_*ock 6

要使用变量函数,可以先构建函数名并将其放在变量中,然后调用它(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()尽管如此.即使你想自定义函数来涵盖lowerupper,变量函数的调用是不必要的.