2012-08-14 75 views
0

我想獲得一個函數輸出文本之間像下面。但它總是在最上面。任何想法如何設置這個權利?它應該是Apple Pie,Ball,Cat,Doll,Elephant,但是玩偶總是在最上面。獲取函數輸出文本之間

function inBetween() 
{ 
echo 'Doll <br>'; 
} 

$testP = 'Apple Pie <br>'; 
$testP .='Ball <br>'; 
$testP .='Cat <br>'; 
inBetween(); 
$testP .='Elephant'; 

echo $testP; 

回答

6

該函數在屏幕頂部回顯,因爲它正在首先運行。您正在附加到字符串,但不會在函數運行之後才顯示它 - 它首先輸出回顯。嘗試返回值是這樣的:

function inBetween() 
{ 
    return 'Doll <br>'; 
} 

$testP = 'Apple Pie <br>'; 
$testP .='Ball <br>'; 
$testP .='Cat <br>'; 
$testP .= inBetween(); 
$testP .='Elephant'; 

echo $testP; 

編輯:您還可以通過引用傳遞這將這樣的工作:

function inBetween(&$input) 
{ 
    $input.= 'Doll <br>'; 
} 

$testP = 'Apple Pie <br>'; 
$testP .='Ball <br>'; 
$testP .='Cat <br>'; 
inBetween($testP); 
$testP .='Elephant'; 

echo $testP; 

雖然傳遞變量的函數發送一條副本,使用函數聲明中的&將其自身發送給變量。該功能所做的任何更改都將作爲原始變量。這將意味着函數會附加到變量上,並且最後會輸出整個事物。

0

相反回聲使用return 'Doll <br>';,然後$testP .= inBetween();

0

那是因爲你是你echo $testP之前運行inbetween()

嘗試:

function inBetween() 
{ 
return 'Doll <br>'; 
} 

$testP = 'Apple Pie <br>'; 
$testP .='Ball <br>'; 
$testP .='Cat <br>'; 
$testP .=inBetween(); 
$testP .='Elephant'; 

echo $testP;