Tips for optimizing your PHP code

June 2, 2017

There is no doubt about PHP being one of the most popular and most widely used development language for various websites and applications. Let’s make our development practices much more efficient by using some of the pro-tips, as shared by experienced PHP developers:

1. When using a loop, if your condition uses a constant, put it before the loop. For instance

for ($i = 0; $i < count($my_array); $i++)

This will evaluate count($my_array) every time. Just make an extra variable before the loop, or even inside :

for ($i = 0, $count = count($my_array); $i < $count; $i++)

2. echo is better than print.

echo (‘code’);
print (‘code’);

3. Use single quotes ( ‘ ) instead of double quotes(“ “  ) will make the things faster  since PHP looks for variables inside double quotes but not inside single quotes. if you are going to keep only the string inside it avoiding any variables. Double quotes checks for the presence of variable and adds little bit of overhead.

4. Use functions outside the loops

5. Use “= = =” instead of “= =”, as the former strictly checks for a closed range which makes it faster.
if, elseif and else (using ===)

6. Always use curly brackets in control structures, even if they are not needed. They increase the readability of the code, and they give you fewer logical errors.
So, for example, the following is incorrect:

if ($foo)
$bar = true
This should be formatted like this:
if ($foo) {
$bar = true

7. Avoid string concatenations in loops

On being placed in a loop, the string concatenation leads to creating many temporary objects and this follows unnecessary use of the garbage collector. Both are straining in terms of memory consumption which can slow down the execution of the script with drastic measures.

You must be aware that your PHP scripts get recompiled each time in case the scripts are not cached. So, it can be great if you install a PHP caching product for an enhanced performance (the performance typically increases by 25-100%). This is achieved by removing compilation times. OP code caches eliminate the need for you to compile the script on each request.

8. else if” statements are faster than “switch/case” statements.

9. Use tags when declaring PHP as all other styles are depreciated, including short tags
10. $record[’id’] is 7 times faster than $record[id]
11. reuse the code

12.Apache serves the PHP script about 2-10 times slower than any static HTML page, so, it is advisable to focus on using more of the static HTML pages and less of the scripts.

13. Methods in derived classes run faster than ones defined in the base class.
14. Use echo’s multiple parameters instead of string concatenation

echo ($a1,$a2,$a3)

15. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4.

At the end, we would like to disclaim that these are only a few good tips that a PHP Developer can follow to fine-tune their Development practices and there can be many more ways you can improve upon your skills. With this short write-up, we have tried to capture some that can have the most impact.

Have more tips to add? Want to learn all about how we approach a PHP project?
Feel free to leave us a comment or contact us.