2016-10-03 152 views
-2

我需要給函數作爲參數函數。 例如函數作爲函數中的參數

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

調用未定義功能'arg12()'那麼,是'arg12()'函數 – RiggsFolly

回答

0

可用性單獨創建的功能!

function smooth($sum) { 
    return sin(rad2deg($sum)); 
} 
$smoothFunc = smooth(10); 
0
<?php 
function arg1($a,$b){ 
    return $a+$b; 
}; 

function smoothFunc($pArg1, $pDx){ 
    return $pArg1+$pDx; 
}; 

echo (smoothFunc(arg1(2,3),1)); 
2
<?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)); 

pleae不assine函數爲變量,並創建第一個地方"$"的功能。

更多詳情這裏http://php.net/manual/en/functions.user-defined.php

+0

可以分配一個功能到JavaScript變量。 – ABHIJIT