Better echo in PHP by replacing . by ,
How to better echo in PHP? With this simple trick, avoid using lots of memory at the last minut.

How many improvement can you spot in this one liner ?

echo ("this ".$will." be displayed.");

No need for parenthesis with echo

The first, and probably the most obvious, are the useless parenthesis. The echo is a ‘structure of language’, aka a special kind of function for PHP : among others, it doesn’t need any parenthesis. This is also the case for inclusions, return or print. This code above is the same as :

echo "this ".$will." be displayed.";

The parenthesis have no effect here, even if the two extra tokens means some extra work for PHP. The impact may be felt in other rare situations where the parenthesis will force the result to be passed by value, and not as a reference, or if the echo call is followed by other operators.

return (&$x); // always returns a value, not a reference
include('my_file.php') || print("error loading"): // always produce an error and an message.

The main effect in this code is to make the reader feel that ‘echo’ is some kind of function. It masks the fact that echo accepts an arbitrary number of arguments. Indeed, the following is not possible with the parenthesis :

echo ($a, $b, $c); // syntax error

echo $a, $b, $c; // but this works

No need for concatenation

Multiple arguments with echo is seldom used, and this is where one can better echo in PHP. The concatenation is actually useless. How does PHP process this line ?

echo "this ".$will." be displayed.";

PHP finds the ‘echo’ so it understands that the following expression will be displayed, which is sent to the output. Then, it finds the concatenation : to process it, PHP allocate the memory necessary for the three strings together (here, 4 + strlen($will) + 14), then copies the values from the strings/variables to the new memory location. Then, it gives the whole result to echo. In a snap, this line has doubled the amount of memory required by the script, right at the last moment.

On the other hand, when providing the strings as separate arguments, echo will immediately send them to the output, without any extra memory allocation.

echo "this ", $will, " be displayed.";

This is also true for concatenations such as :

echo "this $will be displayed.";

Better echo than print ?

You can easily save some or more memory by using echo as a multiple argument function and avoiding the last-minute concatenation. This may be especially true with templating systems, that collect fragments of content along the way and finally output it all in one piece; it is also true with late addition like a new line :

echo $message."\n"; // memory += strlen($message) + 1

This may also be a noteworthy advantage of echo over print : print is a structure of language just like echo, but will only accept one argument, thus, requiring the concatenation or several calls.

Yes to multiple argument echo, no to print and parenthesis!

One thought on “How to better echo in PHP

  1. Pingback: 更好的PHP Echo - Exakat

Comments are closed.