简体   繁体   中英

Can a function be passed as an argument in PHP?

I am wanting to pass a functon as an argument using PHP, in the equivalent way that jQuery allows passing of functions.

jQuery:

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

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

So, I tried this using 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);

... but I get "world!Hello" returned .... why does it return the outcome backwards ?

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.

改变这一行

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM