lambda在PHP中有什么用? [英] What use is lambda in PHP?

查看:92
本文介绍了lambda在PHP中有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lambda匿名函数是PHP 5.3的一部分.它有什么用?有什么只能用lambda做的吗? lambda是否对某些任务更好?

The lambda anonymous function is part of PHP 5.3. What use does it have? Is there anything that one can only do with lambda? Is lambda better for certain tasks?

我已经看过Fibonacci示例,而且我真的不需要编写Fibonacci序列,因此我仍然不确定它是否对我编写Webbish应用程序时遇到的各种任务有用.那么,在现实生活"中该如何处理?

I've seen the Fibonacci example, and I really don't need to write Fibonacci sequences, so I'm still not sure if it's that useful for the kinds of tasks I encounter in writing webbish applications. So what does one do with it in "real life"?

推荐答案

任何需要临时功能的东西,您可能只会使用一次.

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

例如

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

在5.3之前,您必须..

Before 5.3, you'd have to..

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

或create_function ...

Or create_function ...

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

这篇关于lambda在PHP中有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆