我需要将函数作为参数函数。 例如
<i>$smoothFunc = smooth(function ($sum) {
return sin(rad2deg($sum));
}, 15);
$ smoothFunc(10)//~0.438
我怎么能这样做?
此代码不起作用
$arg1=function($a,$b){
return $a+$b;
};
function smoothFunc($arg1, $dx){
return $f1($a,$b)+$dx;
};
echo (smoothFunc(arg1(2,3),1));*/
答案 0 :(得分:3)
<?php
function arg12($a,$b){
return $a+$b;
};
function f1($a,$b){
return $a+$b;
}
function smoothFunc($a, $b, $dx){
return f1($a,$b)+$dx;
};
echo (smoothFunc(arg12(2,3), 2, 1));
请勿将函数绑定到变量中,并使用第一个位置"$"
创建函数。
答案 1 :(得分:0)
单独创建功能以实现可用性!
function smooth($sum) {
return sin(rad2deg($sum));
}
$smoothFunc = smooth(10);
答案 2 :(得分:0)
<?php
function arg1($a,$b){
return $a+$b;
};
function smoothFunc($pArg1, $pDx){
return $pArg1+$pDx;
};
echo (smoothFunc(arg1(2,3),1));