2012-08-14 74 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;