Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,函数可以作为参数传递吗?_Php_Function_Arguments - Fatal编程技术网

在PHP中,函数可以作为参数传递吗?

在PHP中,函数可以作为参数传递吗?,php,function,arguments,Php,Function,Arguments,我希望使用PHP将functon作为参数传递,与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 $fun

我希望使用PHP将functon作为参数传递,与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);

。。。但我得到了“世界!你好”的回复。。。。为什么它会向后返回结果?

好的,我找到了答案。那是因为我在试着回音!我更改了它,以便:

$arg2 = function($name){return $name;};
此输出“Hello world!”与预期一致。

更改此行

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

在某些情况下,没有解释的代码通常不是很有用。请考虑添加它。