Performance is very important for dynamic websites and for PHP apps (like WordPress plugins and themes).
However, speed is not the only parameter to consider when coding. You need to balance performance with other factors, like readability and scalability.
And this is the problem with many PHP performance "tips". Not only do they provide very little speed increase (if any), but they also come at the expense of code quality and readability.
For example, are single quotes faster than double
quotes? Maybe. But the speed increase is so small that it's not even noticeable. If you prefer double quotes to keep your code more readable, is it worth switching to single quotes only for that minimum performance gain?
It doesn't make much sense.
That said, there are some performance tips that actually make sense, because they offer a true performance gain without impacting your code quality.
So, here are 4 useful PHP performance tips that I selected for you.
1. Don't repeat your code inside loops.
Code inside loops (for, while, foreach...) is executed as many times as the loop itself. A piece of code that runs in 1 millisecond will cause a full second delay if it runs 1000 times.
For this reason, you want to keep as much code outside of the loop as possible. A common mistake is to keep some code in the loop even if there is no need to. For example:
for ($i = 0; $i < strpos($letter, $string); $i++){ echo $letter . ' not found at position ' . $i; }
The statement strpos($letter, $string) is executed at every loop. If $string is 1000 characters long, strpos() is executed up to 1000 times! You should move it out of the loop, so it will be executed only once:
$pos = strpos($letter, $string); for ($i = 0; $i < $pos; $i++){ echo $letter . ' not found at position ' . $i; }
2. Use native PHP functions when possible.
Before implementing your own functions, always check if a native PHP function that does what you need already exists. For example, if you need to create a sequence, do not create it manually with a loop. Use the range() function instead.
The reason is that many PHP native functions are optimized for efficiency and speed, and they perform far better than a PHP-based implementation.
Sometimes, the performance difference can be huge. For instance, using range() to create an integer sequence is about 5 times faster than using a for loop.
Using PHP native functions also makes your code more readable and lower the chances of introducing new bugs.
If you can choose between more than one PHP function, go with the fastest one that works for you. For example, str_replace() is faster than preg_replace().
3. Let the database do the hard work.
SQL queries are an important part of any web application. Databases are very good at reading and selecting data, as long as the database structure and the queries you use are optimized.
However, sometimes we don't know how to optimize our SQL queries. Or we simply fear that a complex query may be too slow. And so, we get too much data from the database and we select the data we actually need with PHP.
But using loops, comparisons and other PHP logic to select the data we want is usually much slower than letting the database do
the work. In fact, databases are optimized to do exactly that. You just need to make sure to optimize your SQL queries.
The performance difference may be small if your data consists of just a few rows. But when you have hundreds or thousands of rows, the speed increase can become huge.
4. Profile your code before deciding what to optimize.
My last piece of advice is this: before you optimize your
code, make sure to know what to optimize.
If you are a perfectionist like me, I'm sure you like the idea of having all your PHP code optimized. But your time is valuable. You should not waste it optimizing code that doesn't need to be optimized at all.
If a PHP script is running slow, profile it to check exactly which pieces of code are slowing it down.
That's all for today.
Nowsend me a reply and let me know what you think. I read all the emails.
How do you learn PHP fast, even if you have no experience? Here's how to go from zero to PHP developer in just 9 days. Take a look if you are a PHP beginner.
How do you make your PHP apps secure from attacks? Learn how to secure your code by using the right defense techniques (and stop worrying). Make your code secure.