PHP Perfomance optimization

Valerio Barbera

Hi, I’m Valerio, software engineer, founder and CTO at Inspector.

Once the initial setup 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 optimize PHP perfmance but I think that in most cases it’s enough to start from a few but important points.

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

PHP Tuning: start with PHP 8.x

The latest version of PHP is the fastest version of PHP ever released. According to some reviews, PHP 8 is much faster and secure than PHP 5.

There’s a good backward compatibility so the upgrade can’t require a lot of effort.

You will be able to get the advantage of spending time only once, and reduce the resources needed by the application to run. 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).

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;

ORMs are tools. They make our job easier, like all tools they offer many features. It is up to us to learn how to use them correctly.

Learn more how to use the ORM at scale reading this article:

PHP optimization tips

Use Composer optimized autoload

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

The Composer documentation perfectly exaplains the reason:

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.

Variables are ignored using single quote. You can concatenate multiple 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 executed it’s parsed and compiled into opcode. 

OPCode is an abbreviation of “operation code”, also known as “byte code” in other programming languages.

The process compiles human-readable PHP code to code your server understand. This occurs when the PHP file loads for the first time. Then, it’s saved to the server’s memory for faster loading at each subsequent execution.

PHP provides many functions to have more control on the OPCode cache. The most popular frameworks have packages to simplify interaction with these API.

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.


I hope that one or more of these tips can help you to create more solid and scalable software products.

There are many other factors that can affect the performance of an application. Database interactions, server resources, and many more. Receive more insights by subscribing to our newsletter.

Application monitoring

If you found this post interesting and want to drastically change your developers’ life for the better, you can give Inspector a try.

Inspector is an easy to use Code Execution Monitoring tool that helps developers to identify bugs and bottlenecks in their application automatically. Before customers do.

screenshot inspector code monitoring timeline

It is completely code-driven. You won’t have to install anything at the server level or make complex configurations in your cloud infrastructure.

It works with a lightweight software library that you can install in your application like any other dependency. Check out the supported technologies in the GitHub organization.

Create an account, or visit the website for more information:

Related Posts

How to make Vite Hot Module Replacement work on Windows

As many of our community members already know, we recently started the renovation of the Inspector dashboard UI with a fresh new design and a modern technology stack. In this article I will explain why we decided to leave Webpack and embrace Vite as assets build tool and Hot Module Replacement. I will show you

Http traffic monitoring for Slim framework

This article follows the release of the first version of the monitoring library for Slim framework. Thanks to this package you can fully monitor the HTTP traffic against your application based on Slim. It takes less than one minute to get started. First let me give you a bit of context. Introducing the Slim framework

How to use wildcards in ExpressJs and Fastify monitoring libraries

With the booming software economy, the demand for new applications and software-defined automations is set to grow in the future years. Companies in any industry are creating software in the form of internal business tools, productivity tools, integrations, automations, and more. As a result, there is a need for tools that make it easy to

How to build scalable applications

Get the e-book about the Inspector scalability journey.