Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create dynamic php function and run it later - Save it to a variable

Im trying to make a function to run it later.

In php 5.3.2-1 it works fine. But in 5.1.6 it doesn't.

The code is:

$func = function(){                                                             
  echo "Hello!";                                                                
};                                                                              

echo "Before Hello";                                                            
$func();   

Does anyone knows how to emulate this in 5.1.6?

Thanks.

Eduardo

like image 727
while true Avatar asked Jul 13 '26 05:07

while true


1 Answers

$func = create_function('','echo "Hello!";');

echo "Before Hello";
$func();

to be able to "assign a function to a variable", while not delving into eval-ish code:

function my_not_so_anonymous_function1()
{
    echo "Hello!";
}
$func = 'my_not_so_anonymous_function1';

echo "Before Hello";
$func();
like image 96
mvds Avatar answered Jul 14 '26 18:07

mvds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!