4

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 ?

2 Answers2

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.

2

Change this line

$arg2 = function($name){return $name;};
Saranya Sadhasivam
  • 1,296
  • 6
  • 9
  • Code without an explanation is typically not very useful safe for a few cases. Please consider adding it. – Ja͢ck Jul 31 '13 at 11:42