将函数名称作为参数传递给函数

pot*_*mic 5 php

我有这样的功能:

function test($str,$func) {
    $func($str);
    //Something more here
}
Run Code Online (Sandbox Code Playgroud)

我怎么能在那里传递一个函数名?我想做test("Hello",strtolower);或喜欢test("Bye",sometextprocessfunc);

Jom*_*per 7

function test($str, $func){
    return call_user_func($func,$str);
} 
Run Code Online (Sandbox Code Playgroud)

像这样使用它

$asd = test("ASD","strtolower");
Run Code Online (Sandbox Code Playgroud)


Max*_*sky 5

与任何其他字符串一样

test("Hello","strtolower");
Run Code Online (Sandbox Code Playgroud)

查看变量函数

  • 你为什么不试试? (3认同)