Php 获取介于两者之间的函数输出文本

Php 获取介于两者之间的函数输出文本,php,Php,我试图得到一个函数输出文本,如下所示。但它总是在顶端结束。你知道怎么纠正吗?它应该是苹果派,球,猫,娃娃,大象,但娃娃总是在顶部结束 function inBetween() { echo 'Doll <br>'; } $testP = 'Apple Pie <br>'; $testP .='Ball <br>'; $testP .='Cat <br>'; inBetween(); $testP .='Elephant'; echo $test

我试图得到一个函数输出文本,如下所示。但它总是在顶端结束。你知道怎么纠正吗?它应该是苹果派,球,猫,娃娃,大象,但娃娃总是在顶部结束

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

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

echo $testP;
中间函数()
{
回音“Doll
”; } $testP='Apple Pie
'; $testP.='Ball
'; $testP.='Cat
'; 中间(); $testP.='Elephant'; echo$testP;
该函数在屏幕顶部回响,因为它是先运行的。您正在追加字符串,但直到函数运行后才显示它-函数首先输出回显。请尝试以下返回值:

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;
中间函数()
{
返回“Doll
”; } $testP='Apple Pie
'; $testP.='Ball
'; $testP.='Cat
'; $testP.=中间(); $testP.='Elephant'; echo$testP;
编辑:您也可以按如下方式通过引用传递:

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;
中间的函数(&$input)
{
$input.=“娃娃
”; } $testP='Apple Pie
'; $testP.='Ball
'; $testP.='Cat
'; 中间($testP); $testP.='Elephant'; echo$testP;

将变量传递给函数时,会向函数发送一个副本,使用函数声明中的
&
将变量本身发送给函数。该函数所做的任何更改都将与原始变量相同。这将意味着函数将附加到变量,并在最后输出整个内容。

而不是echo使用
返回“Doll
$testP.=inBetween()

这是因为您在
echo$testP
之前正在运行
inbetween()

尝试:

中间函数()
{
返回“Doll
”; } $testP='Apple Pie
'; $testP.='Ball
'; $testP.='Cat
'; $testP.=中间(); $testP.='Elephant'; echo$testP;