create_function in PHP

Syntax of create_function

string create_function(string arguments, string code)

Creates an anonymous function with the given arguments and code; returns a generated name for the function. Such anonymous functions (also called lambda functions) are useful for short term callback functions, such as when using usort()

Example of create_function

$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
// outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599

Leave a Comment