This post is intended for PHP devs. I'll show you four ways to improve the speed of your PHP app easily. I'll skip the part where I tell you to use PHP 7, you must know by now that the speed improvement is dramatic… (and PHP 5.x is EOL anyway so…)

TL;DR: -a flag for composer, use opcache, use template engine cache, use fully qualified function names.

0. Use Composer optimization

The prod and dev environments are a little bit different. It shouldn't be a surprise to you if I tell you that you should not install the dev dependencies in prod, right?

composer install --no-dev

That's basic. But did you know you can optimize the autoloader? Because the classes won't be changing once it's deployed, you can add a flag to composer (-a) that will improve the speed of autoloading:

composer install --no-dev -a

Ok, but what does it do you'll ask?

From the help it says: "the Composer autoloader will only load classes from the classmap". It also implies "optimize-autoloader" so you don't have to add it too. You can see it as a way to say "hey, no more classes will be added so you don't need to scan the filesystem for a new class, just use the ones in the autoloader".

1. Use opcache

When a PHP file is read, it is converted in opcode, and then executed by the engine. Because in prod your PHP files won't change, you don't want to convert them to opcode every single time. Enter opcache.

Using opcache can dramatically increase the speed of your PHP application. Make sure that opcache_enable=1 is uncommented in your php.ini file. It will store the opcode for the executed files.

But don't enable it in your dev environment ;) (unless you enable opcache.validate_timestamps and set opcache.revalidate_freq to 0 (thx u/iluuu))

I recommend the opcache-status tool to monitor the use of opcache (at least in the beginning).

2. Use a template engine with cache

Templating engines like Twig can create a cache of the generated PHP code. The speed gain here can be dramatic. Again, you only want to cache the templates in prod, not in dev. But make sure that the cache is setup. For Twig see the documentation.

3. Use a backslash in front of standard functions

Normally your code is namespaced (right?). So what happens when you call a function from the standard PHP library? The "compiler" (opcode producer) will look into the current namespace, then go up, and eventually use the global namespace. This means that if you add a "\" in front of standard functions (so effectively namespacing it in the global namespace explicitely), it will result in less opcode instructions, and that means faster code execution. Think it's one of those useless micro-optimization like the use of single vs. double quotes? Think again. Let's take this code:

Without a backslash on the count() :

With a backslash in front of count() :

As you can see, the difference is glaring and if you reproduce this on all the functions in your code it might make a palpatable difference at the end of the day ;)

Conclusion

The little things I'm showing in this post won't make your app faster, at best it'll improve marginally. You can consider them as "good practice" because why not take some speed gains if you can, but if you want to get serious about optimizing your PHP app, get a profiler and optimize your SQL queries, because that's what is causing an issue, not the few milliseconds you might get with this kind of things ;)

That's all folks, have fun coding!

And if you like PHP, I'm always happy with contributions on the eLabFTW project, an open source electronic lab notebook. =)