Tips for faster PHP scripts
Hey there! Welcome to Making the Web - my personal blog about website development. Feel free to subscribe to my RSS feed to keep up with the latest. Alternatively, subscribe by email. Hope you enjoy this article!
This post is part 1 of a 3 part series. For the other parts, visit these posts:
More Tips for faster PHP scripts
Even More Tips for faster PHP scripts
I have listed my top tips for writing faster (optimized) PHP code:
1. Multiple arguments with echo
When you use echo, you probably use something like this:
echo $variable1 . 'string1' . $variable2 . $variable3;
But, you may be forgetting that echo can take multiple arguments. So you can write it like this:
echo $variable1 , 'string1' , $variable2 , $variable3;
Passing multiple arguments to echo is faster than joining the strings first, and then passing them to echo. I done a little test in PHP. I echoed 10 different strings 500 thousand times, firstly with the concatenation (joining) method, and then with the multiple arguments method. Here are my results:
Time for concatenation method: 37.83755 seconds
Time for multiple arguments method: 37.68789 seconds
Time saved: 0.15966 seconds; 0.396%
As you can see, the difference is very small. It is not worth going over all your old PHP scripts and changing the dots to commas, unless you are extremely desperate for speed. It is more a case of preference, than there being a best way.
Oh, and another small tip (without it's own number): use echo instead of print - it's faster!
__('Read the rest of this entry »')











