PHP Performance Optimization

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

Once the initial construction phase is over and the new web site or application starts to serve real users, then this is the time when stability and reliability plays a critical 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 personal checklist hoping that it will be a starting point for you to make some improvements.

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, 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.

SQL Queries improvements

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

40/50K queries per hour 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 10% 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;

I wrote a complete step by step guide to improve your ORM performance and build sustainable applications. Learn more on this article: https://www.inspector.dev/make-your-application-scalable-optimizing-the-orm-performance/

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

OPcache configuration

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.

Every time a PHP script is requested it will be parsed and compiled into opcode which then is executed in the Zend 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 see those changes immediately.

However, the parsing and compiling steps increase your response 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

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.


Without Inspector it would not be possible to have much of this information about the health of an applications.

If you want to learn more about our product take a look on how it works:

Discover Inspector to avoid losing customers and money

Learn more