PHP Perfomance optimization

Valerio - Jun 18 '19 - - Dev Community

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 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 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 hour. 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 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;
}
Enter fullscreen mode Exit fullscreen mode

Single quote wins on Double quotes (maybe)

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
Enter fullscreen mode Exit fullscreen mode

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:

// faster echo
echo 'Valerio';

// slower echo
echo "Valerio";
Enter fullscreen mode Exit fullscreen mode

This is not true in case of string concatenation, for which it is exactly the opposite.

$name = 'Valerio';

// slower echo
echo 'Hello ' . $name;

// faster echo
echo "Hello $name";
Enter fullscreen mode Exit fullscreen mode

You can check the complete test here (thanks to Andrey Ageev for reporting me): https://www.php.net/manual/en/language.types.string.php#120160

Single quotes are easier on the server but only for literal string (without variables concatenation). Since PHP does not need to read the whole string in advance, the server can work faster and happier.

When you need to build a string by variables concatenation double quotes are finally faster.

OPcache configuration

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.

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


I’m creator of Inspector a Real-Time monitoring tool for Laravel developers and teams.

Inspector identify issues before your users are aware of them, keeping your business safe.

Try Inspector, it’s free: https://www.inspector.dev

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .