I’m trying to get a functions output text in between like below. But it always ends up on the top. Any idea how to set this right? It should be Apple Pie, Ball, Cat, Doll, Elephant, but the Doll always ends up on the top.
function inBetween()
{
echo 'Doll <br>';
}
$testP = 'Apple Pie <br>';
$testP .='Ball <br>';
$testP .='Cat <br>';
inBetween();
$testP .='Elephant';
echo $testP;
The function is echoing at the top of the screen because it is being run first. You are appending to the string, but you don’t display it until after the function is run – which outputs the echo first. Try a return value like this:
Edit: you could also pass by reference which would work like this:
While passing a variable to a function sends it a copy, using an
&in the function declaration send it the variable itself. Any changes made by the function are made the the original variable. This will mean that the function appends to the variable and the whole thing is output at the end.