DEV Community

Cover image for PHP Perfomance optimization
Valerio for Inspector

Posted on • Updated on • Originally published at inspector.dev

PHP Perfomance optimization

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

Top comments (6)

Collapse
 
nemutaisama profile image
Andrey Ageev

Single quoted string was faster ages ago. Not sure but think it was before 5.3. Anyway - when we talk about 5.6+ PHP versions parsing double quotes is faster then concatenation.
php.net/manual/en/language.types.s...

Collapse
 
ilvalerione profile image
Valerio

Hi Andrey! Thanks for sharing, it's a really interesting test. I reproduced the test in my local environment with PHP 7.1 and I had the same result. It seams that for the last PHP release (PHP 7.x) something is changed. I'm going to update the article.

Collapse
 
nemutaisama profile image
Andrey Ageev

Hey! Did it with pleasure, just because "single vs double quotes" thing makes me crazy. There is another old good article, that shows difference from inside nikic.github.io/2012/01/09/Disprov...

And just to make examples fully correct - concatenation is bit faster with only one variable, but it slows down with each variable you add.

And after all - according to test on php.net, using single quotes instead of double quotes is 0,5ms faster for 1 million strings, but, how many strings you have in your project? If amount for each page load is close to at least 10k strings - maybe it's time for some refactoring?

Thread Thread
 
ilvalerione profile image
Valerio • Edited

In my way of thinking it's more about habits than refactoring. I study new things and write articles to improve my habits. If my skills improve, then slowly the applications I'm working on will change for the better.

I don't think the impact on performance is business critical, so I don't think a refactoring task is necessary. Invest in skills and you (or your collaborators) will get the same result automatically.

Thread Thread
 
nemutaisama profile image
Andrey Ageev

I understand your point, and fully agree. I think it's again problem that i can't explain what's on my mind, damn my bad english ). I just trying to say that it's not so much strings in enough good designed project, and using "fastest" quotes for each case won't give you real profit. IMHO good choice is select quotes for better readability and team codestyle.

Thread Thread
 
devmount profile image
Andreas

*sneaks in and whispers* As long as no variables are replaced, there is no significant difference between single and double quotes. I did a quick benchmark today with PHP 7.4:

See discussion in my recent article about PHP performance optimization:

*sneaks out*