I have made a small function (WordPress), using echo .
/* .. Some code */
switch ($linktype) {
case \"next\":
echo \'
Commas are faster.
The echo
construct allows multiple "parameters". When you echo
with commas, the output is sent straight to the buffer piece by piece. When you use .
, it has to concatenate first.
This won't make a huge dent in speed for most applications, but I generally make it a habit to use commas for echo
anyway.
Here's a benchmark, if you're curious: http://www.electrictoolbox.com/php-echo-commas-vs-concatenation/
EDIT: Now, here's why things are "out of order". (Apologies to all, as I just now figured out that this was the root question the whole time.) When you echo
with .
, you concatenate first before echo
gets to do its job. To do that, each expression needs evaluated first. Consider this:
echo (5+5) . (10+10);
PHP will first evaluate (5+5)
and then (10+10)
. This is equivalent to turning it into this:
echo 10 . 20;
And then these need concatenated, so they are converted to strings and become this:
echo "1020";
Does that make sense? Now consider the function previous_post_link()
. @Tim is quite right that there is no return value from this function. When that function is evaluated, it returns nothing and echos something. So if we do this:
echo "test" . previous_post_link();
First, both things are evaluated. "test"
is already a string, but we need to run the function previous_post_link()
first to get its return value for concatenation. When ran, previous_post_link()
outputs something, and returns nothing. "test"
is then concatenated with nothing, and that concatenation is output via echo
.
Now, suppose we use commas instead:
echo "test", previous_post_link();
PHP evaluates all of the "parameters" for the echo
construct in order, and outputs them. First, "test"
is output, and then previous_post_link()
is evaluated, which has its own output, and returns nothing, so nothing is output for it.
I hope this is clearer. Post if not.