What use is lambda in PHP?

Anything that requires a temporary function that you probably will only use once.

I would use them for callbacks, for functions such as:

  • usort
  • preg_replace_callback

E.g.

usort($myArray, function ($a, $b) {
    return $a < $b;
});

Before 5.3, you’d have to..

function mySort ($a, $b) {
    return $a < $b;
}
usort($myArray, 'mySort');

Or create_function …

usort($myArray, create_function('$a, $b', 'return $a < $b;'));

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)