在函数中起参数作用

时间:2016-10-03 16:44:43

标签: php function arguments

我需要将函数作为参数函数。 例如

<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));*/

3 个答案:

答案 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));

请勿将函数绑定到变量中,并使用第一个位置"$"创建函数。

此处更多详情http://php.net/manual/en/functions.user-defined.php

答案 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));