开发者

lambda-style function in php

开发者 https://www.devze.com 2023-02-01 20:26 出处:网络
See the code below. $newfunc = create_function(\'$a,$b\', \'return \"ln($a) + ln($b) = \" . log($a * $b);\');

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

0

精彩评论

暂无评论...
验证码 换一张
取 消