4

我想使用 PHP 将函数作为参数传递,就像 jQuery 允许传递函数一样。

jQuery:

$("#foo").bind("click", function(){

    alert("Hello world!");
});

因此,我使用 PHP 进行了尝试:

$arg1 = "Hello";
$arg2 = function($name){echo $name;};

function call_me($func_arg1="", $func_arg2=""){

    echo $func_arg1." ".$func_arg2("world!");
}

call_me($arg1, $arg2);

...但我得到“world!Hello”返回......为什么它会向后返回结果?

4

2 回答 2

3

ok, I found the answer. It was because I was trying to echo an echo! I changed it so that:

$arg2 = function($name){return $name;};

This output "Hello world!" as expected.

于 2013-07-31T11:36:27.023 回答
2

Change this line

$arg2 = function($name){return $name;};
于 2013-07-31T11:36:30.190 回答