PHP Perfomance optimization

Valerio Barbera

Once the initial construction phase is over and the application start to serve real users, then this is the time when stability and reliability plays an important role as well as the speed of development.

There are hundreds of ways to do some sort of performance optimization in PHP applications but I think that in most cases it is sufficient to start from a few but important points.

In this article I show you my checklist about php performance tuning, hoping that it will be a starting point for you to make some improvements.

PHP Tuning: start with PHP 7.x

The latest version of PHP is the fastest version of PHP out there. According to some reviews, PHP version 7 is much faster and secure than PHP 5.

There’s a good Backward compatibility so in case your app need some days to adapt some lines of codes it will be really worth it.

You will be able to get the advantage of spending a little time only once, but by reducing the resources needed by the application to work well, you could cut the cost of your hosting by a few percentage points in the long-term.

Annual benchmark provided by Kinsta (the best WordPress hosting in the world) tell us how huge the performance increase is:

The Definitive PHP 5.6, 7.0, 7.1, 7.2 & 7.3 Benchmarks (2019)

SQL Queries improvements

I worked on a project where the backend runs 40/50K queries per second. Using an ORM (Eloquent in that case) it couldn’t easy to know if some ORM instruction can generate more query than needed.

40K/50K queries per second was an unexpectedly high number. After a little investigation I changed one line of code using Eager Loading to preload a relationship to make the application 30% faster.

// Slower
$books = App\Book::all();
foreach ($books as $book) {
  echo $book->author->name;

// Faster (using Eager Loading)
$books = App\Book::with('author')->get();
foreach ($books as $book) {
  echo $book->author->name;

PHP optimization tips

Use Composer optimized autoload

If not optimized a php application could spending quite some time in the autoloader instead of doing more interesting things.

The reason is perfecly explained in the Composer documentation:

Due to the way PSR-4 and PSR-0 autoloading rules are set up, it Composer needs to check the filesystem before resolving a class name conclusively. This slows things down quite a bit, but it is convenient in development environments because when you add a new class, it can immediately be discovered/used without having to rebuild the autoloader configuration.

In production instead we should prefer the optimized version of the classmap because the code will change only on the next deployment when a new dump-autoload will be executed again.

Single quote wins on Double quotes

Using double quote you can insert variables directly within the text of the string. The PHP parser will automatically detect such variables, convert their values into readable text, and place them in their proper places.

$name = 'Valerio';

echo "Hello $name"; // Output: Hello Valerio

PHP takes longer to process double quoted strings. Since the PHP parser has to read the whole string in advance to detect any variable inside — and concatenate it — it takes longer to process than a single quoted string.

Using Single quote the variables are ignored and you can concatenate variables with a string by using the dot notation.

$name = 'Valerio';

echo 'Hello $name'; // Output: Hello $name

echo 'Hello '. $name; // Output: Hello Valerio

Single quotes are easier on the server. Since PHP does not need to read the whole string in advance, the server can work faster and happier.

Make php even faster

Every time a PHP script is requested it will be parsed and compiled into opcode which then is executed in the PHP Engine. This is what allows PHP developers to skip the compilation step required in other languages like Java or C# — you can make changes to your PHP code and use those changes immediately.

However, the parsing and compiling steps increase the execution time, and in a non-development environment are often unnecessary, since your application code changes infrequently.

OPcache leverages a cache for this “bytecode”, so the next time the same script is requested, it doesn’t have to recompile it. This can save some precious execution time, and thus make your app faster.

For Laravel

Looking for php optimizazion settings for Lavarel? Working in Laravel based applications for the most of time I can suggest you an handy package that gives you Artisan commands to work with OPcache.

For Symfony

In the official Symfony documentation there’s a good step by step guide that help me in the past to optimize Symfony execution including OPcache configuration.

Related Posts

How I handled the scalability of the SQL database in Inspector

Hi, I’m Valerio software engineer, CTO and founder at Inspector. In this article I’ll talk about what I learned trying to increase the operational limits of the Inspector SQL database. Before talking about read-replicas or sharded-data, it might be helpful to introduce the problem to solve and the most common strategies to improve the ability