Can a function be passed as an argument in PHP?

I want to pass functon as an argument using PHP in an equivalent way that jQuery allows you to pass functions.

JQuery

$("#foo").bind("click", function(){ alert("Hello world!"); }); 

So, I tried this with 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 return "peace! Hello" .... why does it return the result back?

+4
source share
2 answers

ok, I found the answer. That was because I tried to drive away the echo! I changed it so that:

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

This conclusion is "Hello world!" as was expected.

+3
source

Change this line

 $arg2 = function($name){return $name;}; 
+2
source

Source: https://habr.com/ru/post/1494479/


All Articles