我可以在PHP中使用运算符作为函数回调吗? [英] Can I use operators as function callback in PHP?
本文介绍了我可以在PHP中使用运算符作为函数回调吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我具有以下功能:
function mul()
{
return array_reduce(func_get_args(), '*');
}
是否可以将*运算符用作回调函数?还有其他办法吗?
Is is possible to use the * operator as a callback function? Is there any other way?
推荐答案
在这种情况下,请使用 array_product()
:
In this specific case, use array_product()
:
function mul() {
return array_product(func_get_args());
}
一般情况下?不可以,您不能将运算符作为回调传递给函数.您至少必须将其包装在一个函数中:
In the general case? No, you can't pass an operator as a callback to a function. You would at least have to wrap it in a function:
function mul() {
return array_reduce(func_get_args(), 'mult', 1);
}
function mult($a, $b) {
return $a * $b;
}
这篇关于我可以在PHP中使用运算符作为函数回调吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文