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.

New to Inspector?

Get a monitoring environment specifically designed for software developers to avoid server or infrastructure configuration.

Developers may be uncomfortable installing and configuring software at the server level. Server level configurations are out of the software development lifecycle or are even handled by external teams.

Thanks to Inspector, you will never need to install things at the server level or make complex configurations in your cloud infrastructure.

Inspector works with a lightweight software library that you can install in your application like any other dependencies based on the technology or framework. Check out our GitHub organization for more information:

Visit our website for more details:

Related Posts

How to configure HTTPS in Laravel Homestead

How to enable HTTPS in Laravel Homestead

Hi, I’m Valerio Barbera, software engineer, founder and CTO at Inspector. In this article I’ll show you how to enable HTTPS for your local applications served by Homestead. I met this need because I am working to implement browser notifications for Inspector using Pusher/Beams. But Beams requires that the application be necessarily served over HTTPS.

Laravel cron scheduling and its secrets

Hi, I’m Valerio Barbera, software engineer, founder and CTO at Inspector. Laravel tasks scheduling is one of the most useful features of the framework.The official documentation clearly explains what it is for: In the past, you may have written a cron configuration entry for each task you needed to schedule on your server. However, this

Laravel validation and custom rules in Inspector

Hi, I’m Valerio Barbera, software engineer, founder and CTO at Inspector. Data validation is one of the fundamental features in any application and it is something developers manipulate almost every day. The value a software provides to users is often a function of the quality of data it is able to manage. Laravel ships with

How to build scalable applications

Read the best news, tips and other direct in your inbox