PHP echo() Simple Performance Enhancing

Not many people seem to know this, but PHP's echo() function (ok, it's not strictly a function) can actually take more than one argument. More importantly, it is more efficient to supply multiple arguments than using concatenation.

Most developers will tend to concatenate the argument to the echo function, like this:

$useful = "useful";
echo "This ".$useful." PHP blog is actually quite ".useful."!";

A more efficient way of achieving the same is:

$useful = "useful";
echo "This ",$useful," PHP blog is actually quite ",useful,"!";

It can save more than 20% in execution time, which may be just what the doctor ordered for larger projects.

It is good to remember here that echo is an input/output process, which tends to consume a lot of execution time. Therefore, at times it can be worthwhile in a script to have one long concatenated string with a single call to echo, rather than a lot of small calls to echo.


No comments:

Post a Comment