PHP Anonymous Functions

2011-10-11

Sweet, but a little awkward. The good news is that PHP supports anonymous/lambda functions (that you can pass around, etc.), the bad news is you pass the function in as a string.OK, I admit the bad news isn’t that bad, but passing as a string could be a let-down if you’ve done much JavaScript.

http://php.net/manual/en/function.create-function.php

<?php<br /> $newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');<br /> echo "New anonymous function: $newfunc\n";<br /> echo $newfunc(2, M_E) . "\n";<br /> // outputs<br /> // New anonymous function: lambda_1<br /> // ln(2) + ln(2.718281828459) = 1.6931471805599<br /> ?>
All syntax aside, I’m impressed PHP has had this since June 2000.
http://php.net/releases/index.php
Written on October 11, 2011