lambda-style function in php
See the code below.
$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
/开发者_Go百科/ ln(2) + ln(2.718281828459) = 1.6931471805599
Can any one tell how come it output lambda_1 when print $newfunc? and different output on second time.
DEMO
Its just that anonymous functions are internally named lambda_1, lambda_2, etc. So you the first echo statement gives "New anonymous function: lambda_1"
The function itself is returning a string, hence the second echo statement (echo $newfunc(2, M_E) . "\n";
) gives ln(2) + ln(2.718281828459) = 1.6931471805599
精彩评论