gpt4 book ai didi

php - 函数可以作为 PHP 中的参数传递吗?

转载 作者:可可西里 更新时间:2023-10-31 22:55:45 26 4
gpt4 key购买 nike

我想使用 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”...为什么它会向后返回结果?

最佳答案

好的,我找到了答案。那是因为我试图 echo !我将其更改为:

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

这个输出“Hello world!”正如预期的那样。

关于php - 函数可以作为 PHP 中的参数传递吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17969188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com