在php的另一個函數中調用函數的語法是什麼? 我想是這樣的:PHP:在另一個函數的參數中調用函數
function argfunction($a,$b,$c){
}
function anotherfunction(argfunction($a,$b,$c), $d, $e)
{
}
我不是在anotherfunction
在php的另一個函數中調用函數的語法是什麼? 我想是這樣的:PHP:在另一個函數的參數中調用函數
function argfunction($a,$b,$c){
}
function anotherfunction(argfunction($a,$b,$c), $d, $e)
{
}
我不是在anotherfunction
函數的參數應該是聲明式的,即它們不應該做某事。
但是你可以用callable
關鍵字做到這一點(PHP 5.4):
function argfunction($a,$b,$c){
return $a+$b+$c;
}
function anotherfunction(callable $a_func, $a, $b, $c, $d, $e) {
// call the function we are given:
$abc = $a_func($a, $b, $c);
return $abc + $d * $e;
}
// call:
anotherfunction ("argfunction", 1, 2, 3, 4, 5); // output: 26
或者你也可以通過全功能的定義:
echo anotherfunction (function ($a, $b, $c) {
return $a+$b+$c;
}, 1, 2, 3, 4, 5); // output: 26
或者,一個函數分配給一個變量,並傳遞:
$myfunc = function ($a, $b, $c) {
return $a+$b+$c;
};
echo anotherfunction ($myfunc, 1, 2, 3, 4, 5); // output: 26
但如果你只是想傳遞一個函數調用的結果到另一個功能,那麼它更直截了當:
function anotherfunction2($abc, $d, $e) {
return $abc + $d * $e;
}
echo anotherfunction2 (argfunction(1, 2, 3), 4, 5); // output: 26
不知道這是不是正確的解釋,但你的意思是函數'anotherfunction($ abc,$ d,$ e)'你真的將'argfunction'的參數串在一起?如果我在'anotherfunction'的函數定義中調用'argfunction',那麼格式應該是什麼? – shoestringfries
不,在調用'anotherfunction2(argfunction(1,2,3),4,5);'時,PHP將首先調用* argfunction *參數* 1,2,3 *,這將返回6.然後PHP會調用* anotherfunction2 *將前面的結果作爲第一個參數傳遞給它,所以傳遞的參數是* 6,4,5 *。這些被分配給變量* $ abc *(名字裏有什麼),* $ d *和* $ e *。 – trincot
這樣的語法不存在.....這是根本不允許的語言.....它甚至應該做什麼? –
使用先前定義的函數的輸出作爲另一個函數的輸入 - 它是不允許的? – shoestringfries
@shoestringfries當你調用函數yes時,但不作爲函數定義。所以呢:'另一個函數(argfunction($ a,$ b,$ c),$ d,$ e)'並且在你的定義中:'function anotherfunction($ fresult,$ d,$ e)' – Rizier123