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: https://inspector.dev/make-your-application-scalable-optimizing-the-orm-performance/

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.

https://github.com/appstract/laravel-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.

https://symfony.com/doc/current/performance.html

Conclusion

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: https://inspector.dev

Related Posts

Python Flask vs Django

Python offers several frameworks to streamline the development process. In this article we compare Flask vs Django, two of the most known frameworks for web development. As anticipated in previous articles we are building our Machine Learning API in Python. The choice of the framework to use was an important step to guarantee the best

Custom Laravel Eloquent Collections – Fast tips

Eloquent is one of the most powerful components of the Laravel framework. It is an Object-Relational Mapping (ORM) tool that simplifies database interactions. Laravel Eloquent provides a convenient way to work with database records through its built-in collections. While Laravel comes with a variety of pre-defined collection methods, you can also create your own custom

What is a SIEM, and how is it used in Cyber Security?

After five years working in the minitoring industry I learned a lot about the impact monitoring platforms has in the Cyber Security posture of software development companies. In today’s interconnected world, the need for robust cybersecurity measures has become more critical than ever before. One essential component of a comprehensive cybersecurity strategy is Security Information