Should you use single quotes or double quotes when writing PHP? How about URLs? Should they be absolute or relative? You make choices like these when writing code and your choice will depend on a any number of factors, one of which is performance.
Last week I talked about the differences between dynamic and static web pages and why the former always take longer to load. I also talked about the Zend Engine that interprets PHP and converts it into machine code and how we can make use of caching to make the process quicker.
Today I want to continue talking about PHP. I want to briefly talk about potential bottlenecks in your code before offering a few tips for writing PHP that executes faster. I’ll point you to some resources for more tips as well.
Find Bottlenecks in Your PHP Code
In a previous post in this series I talked about server hardware, specifically CPU, RAM, and I/O Speed and tried to point out which might be more important depending on the type of site you have. Since I did cover this before, I won’t go into great detail here, but I wanted to mention hardware because it can be the bottleneck. If your code is running slow, it’s worth thinking about hardware and whether or not your site has outgrown the current specs of your server.
You also have to consider third party code that’s out of your control, and where your code calls a database it adds another level of performance bottlenecks. I’ll talk more about database in a few weeks.
When it comes to your code itself there aren’t going to be any one-size-fits-all bottlenecks, since it depends on your specifics. Ideally you want to monitor performance, find where code execution is slow, and do what you can to rework your code to improve it.
You can pay for services that monitor your site all the time and alert you when something needs changing, but not everyone can afford that. There are also some free profiling tools and even simple scripts you can use to test how long blocks of code take to run.
I’m definitely not the right person to explain how these tools work so I won’t even try. Instead I’ll point you to some resources for free and paid tools and services.
- Xdebug Extension For PHP | Documentation
- 50 Extremely Useful PHP Tools
- Monitoring PHP Performance and Diagnosing Bottlenecks
- Finding bottlenecks in your PHP code
Tips for More Performant PHP Code
Ideally you want to monitor and profile your code to find where it can be improved, but I know not everyone is going to do that, especially if you don’t write your own applications from scratch. If you’re like me, you only need to write PHP on occasion and when you do you’re more intent on making it work than figuring out how to make it run faster.
That doesn’t mean you can’t learn a few things so you’ll be more likely to write more performant code the first time around and develop better habits in general. Search the web enough and you’ll find a lot of tips. I’ll present a few I see fairly often and leave you with with some resources to find more.
Make use of native PHP functions instead of writing your own. Odds are the native functions will run faster and will have had more scrutiny and optimization than your custom code. There’s a reason those native functions exist. Spend a little time searching through the documentation to see if something exists before trying to write something custom.
Prefer single quotes to double quotes as double quotes have to check for variables whereas single quotes don’t. If you’re wrapping quotes around a variable, then you obviously need the double quotes, but for everything else single are the preferred option.
Use absolute URLS over relative URLs. That way the system doesn’t have to resolve the URL. You can set up a CONSTANT for the domain root so you don’t have to retype it over and over if you like.
DOMAIN = ‘https://domain.com’;
URL = DOMAIN . “path-to-resource”;
Where possible use fewer loops, especially nested ones. As great as loops are they’re often a cause of bottlenecks in your code. It’s unlikely you’re going to eliminate all loops so know that a foreach loop is faster than a for loop, which is faster than a while loop.
Close database connections and unset variables when they’re no longer needed as both require memory. Closing connections and unsetting variables will free up memory for other parts of your code.
When making a comparison the identity operator (===) will be faster than equality operator (==) as the latter has to convert the data type temporarily to make the comparison. Sometimes you do need the equality operator, but in general remember that three equals signs will run faster than two.
Use isset() over count(), strlen(), and sizeof() when you need to know if a value is greater than 0 as it’s both faster and simpler.
Make use of caching (memcache, APC, and OPcache) when you can. I talked about OPcache last week and caching in general has been a consistent theme of this series and caching will continue to be until it’s finished.
Prefer local variables to global variables when you can use either. Global variables have more overhead than local ones which adds a little time to the execution of your code.
Limit interaction with databases. The greatest bottleneck in your PHP likely has to do with the database. The less you have to interact with the database the better. You can’t stay away from your database entirely or you wouldn’t be using one, but you can be careful not to read from or write to it when you don’t need to.
I’ll leave things there and let you look through some of the resources below if you want more. You can search Google or read through books about PHP and find many more tips and guidelines for writing more performant code. I thought I would point out some common, yet simple things you can do and that you possibly encounter often enough.
Here are a few of the articles I found helpful while researching this post. Again, there’s plenty more that can be easily found if you search for it, but these are some that I collected while writing this post.
- Improving PHP Performance for Web Applications
- An Extensive Checklist Of PHP Performance Optimization Techniques
- PHP Profiling – Performance Optimization Techniques
- 63 Tips for speeding up PHP
- 5 Things You Should Check Now to Improve PHP Web Performance
- Website Performance: PHP
- Improving PHP Performance for Web Applications
Know that sometimes the bottleneck in your PHP code isn’t really in your code at all. It’s possible your network or hardware is what’s causing your site to run slow. Odds are the major bottlenecks will come from interactions with your database as they can account for most of the execution time of any PHP application.
If you can afford it or have the time, you should monitor your site or application to find out where it isn’t performing well. That’s not always realistic for us one person shops so know there are plenty of things you can do to write better code that mostly come down to establishing better habits.
Finally there’s one tip I didn’t mention that might be one of the more important and that’s to upgrade to the latest version of PHP. I didn’t mention it today, because it’s what I want to focus on next week. I want to talk about PHP 7 and some of the reasons it’s much faster than PHP 5 and hopefully convince you that it’s a good idea to upgrade.
Download a free sample from my book, Design Fundamentals.