2012-02-10 44 views
2

我有一個PHP函數,返回的東西:在這裏我想從上面的函數傳遞返回值PHP - 傳遞函數返回另一個函數

function myfunction() { 
    $array = array('one', 'two', 'three', 'four'); 

    foreach($array as $i) { 
    echo $i; 
    } 
} 

而另一功能:

function myfunction2() { 
    //how to send myfunction()'s output here? I mean: 
    //echo 'onetwothreefour'; 
    return 'something additional'; 
} 

我猜它會看起來像myfunction2(myfunction),但我不知道PHP太多,我不能讓它工作。

+2

使用在foreach回一點兒也不好聽。因爲它會返回一次並退出函數,所以它不會到達下一個數組項。 – Orhan 2012-02-10 15:35:05

+0

我不明白你的問題。你能更清楚嗎? – DonCallisto 2012-02-10 15:35:12

+0

我的錯,我的意思是「回聲」! – Wordpressor 2012-02-10 15:35:36

回答

5

是的,你只需要

return myFunction(); 
0

myfunction總是返回"one"。沒有其他的。請修改return的行爲

之後,如果您仍然希望將一個函數的返回值放在另一個函數的內部,就調用它。

function myfunction2() { 
    $val = myfunction(); 
    return "something else"; 
} 
0
function myfunction2() { 

    $myvariable=myfunction(); 
    //$myvar now has the output of myfunction() 

    //You might want to do something else here 

return 'something additional'; 
} 
0

試試這個:

function myfunction() { 
    $array = array('one', 'two', 'three', 'four'); 
    $concat = ''; 
    foreach($array as $i) { 
    $concat .= $i; 
    } 
    return $concat; 
} 

function myfunction2() { 
    return myfunction() . "something else"; 
} 

這將返回onetwothreefoursomthing else

在這裏工作的例子http://codepad.org/tjfYX1Ak

相關問題