Poonam Bhatt
Poonam Bhatt

Reputation: 10322

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
 // 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

Upvotes: 0

Views: 1111

Answers (1)

JP19
JP19

Reputation:

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

Upvotes: 3

Related Questions