0

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

4
  • I don't understand what's wrong? Commented Dec 29, 2010 at 11:23
  • Nothing wrong...just could not understood how it worked. Hope you can help me. Commented Dec 29, 2010 at 11:24
  • what exactly do you mean? What aspect of the function is your question about? Commented Dec 29, 2010 at 11:27
  • @Pekka: My question is, why it returned lambda_1 for echo "New anonymous function: $newfunc\n"; And adding more to it, when i refresh that page it increment that number by one. Commented Dec 29, 2010 at 11:30

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.