I wrote up a function that echoed out the id at the otherside of a linked table.
when I write in the page that's calling the function--
echo "<br/>getalbumartistfunction: ".get_albumartistid($thisalbum);
it returns the artist_id number on the line above where I have that function call.
So I went into the function and switched it from 'echo' to 'return' and it now appears right after the colons like I would expect (and is probably more along the lines of what I need).
So it works. But I am extremely confused why it would show the result on the previous line when the function is set to echo it.
The echo in the get_albumartistid is executed immediately when the function is called while the echo "
..." assembles the string before echoing it - essentially buffering the entire thing before it echoes. Return instead of echo is the proper way to handle this since that would effectively replace the function call with the string returned. If you were to use echo you'd have to:
echo "<br/>getalbumartistfunction: ";
get_albumartistid($thisalbum);
Which effectively becomes:
echo "<br/>getalbumartistfunction: ";
echo "<The artist id>";
In your original example that order of execution makes it:
echo "<The artist id>";
echo "<br/>getalbumartistfunction: ";
Since all echoes happen immediately when it's called and PHP is not done buffering your outer echo statement.
Since the argument for echo
needs to be known before echo
is be called, the function get_albumartistid
is called before. And thus any statement inside the function is also called before the echo
. That’s why an echo
inside the function will output data before the echo
outside the function.
Because the get_albumartistid
function is evaluated before the whole echo is output, that is, the echo inside the get_albumartistid
is executed before the echo "<br/>getalbumartistfunction: "
.
echo
sends content directly to the output buffer. When you have the echo
statement in the function, this is what happens:
- PHP encounters the outer
echo
statement. It starts constructing the string to send to the output buffer. The first thing it sees is the string "<br/>getalbumartistfunction: "
. The next thing, concatenated onto the previous string, is the call to the function get_albumartistid()
. It needs to evaluate this function call to finish evaluating the concatenation, so...
- PHP executes the function
get_albumartistid()
. That function contains an echo
statement. The content of the echo
statement is sent to the output buffer. The function returns null
.
- The function returned
null
, so PHP concatenates null
onto the previous string (leaving it unchanged).
- The string in the outer
echo
is now finished, and echo
is ready to return. It sends the string to the output buffer.
Expression vs. execution order.
Your echo "string" . get()
leads to following execution flow:
- PHP combines the expression following echo. The echo cannot be executed yet.
- The first string in the concatenation is static.
- To get the second string, the function needs to be called. When the function does anything funny (make fart noises or print something out) it does so right away at this point.
- The two string parts are combined into a new temporary string.
- Only now can echo output everything.