How to adjusting PHP OPCache from .user.ini ?

Programming, error messages and sample code > PHP

If you make changes with your PHP files, but it does not take effect immediately, you can refer to the solutions here. This article is a guide about how to use and disable PHP OPCache. 

The opcache.revalidate_freq configuration directive is a setting in PHP's OPCache extension that determines 'How often to check script timestamps for updates, in seconds. 0 will result in OPcache checking for updates on every request'

OPCache is a bytecode cache for PHP, which improves performance by storing precompiled PHP code in memory, reducing the need to recompile the code on each request.

The opcache.revalidate_freq directive specifies the frequency, in seconds, at which OPCache verifies whether the cached version of a file is still up-to-date. 

When a file's modification timestamp changes, OPCache considers it potentially updated and will revalidate the cached version of the file. If the file is determined to be updated, OPCache invalidates the cached bytecode and recompiles it on the next request, ensuring that the latest version of the file is used.

Setting opcache.revalidate_freq to a higher value reduces the frequency of checks, which can improve performance by reducing the overhead of checking file modifications. However, it also means that changes to files may not be reflected in the cached bytecode immediately. 

However, sometimes, you may need to update your PHP files frequently. Thus, you would not wait for mins with each update. In this case, you can try to override this value with a .user.ini file. 

Before set up this value, you should know the meaning of these PHP values: 

opcache.validate_timestamps  (bool)
If enabled, OPcache will check for updated scripts every opcache.revalidate_freq seconds. When this directive is disabled, you must reset OPcache manually via opcache_reset(), opcache_invalidate() or by restarting the Web server for changes to the filesystem to take effect.
opcache.revalidate_freq      (int)
How often to check script timestamps for updates, in seconds. 0 will result in OPcache checking for updates on every request.

This configuration directive is ignored if opcache.validate_timestamps is disabled.
 

Below are the steps to override this value:

1. Create or locate the .user.ini file in the root directory of your PHP application from Control Panel --->Files.

2. Add the following line to the file:

3. Save the .user.ini file.

Once you've made the changes and saved the .user.ini file, PHP will pick up the new configuration the next time a PHP script is executed within the directory where the .user.ini file is located. The opcache.revalidate_freq directive will be set to 2, specifying that OPCache should check for updates to cached files every 2 seconds.

What if you want to disable PHP OPcache? 

By setting opcache.enable to 0, you are instructing PHP to disable the OPCache extension. This means that PHP will not use OPCache to store and reuse precompiled bytecode, resulting in the absence of performance optimizations provided by OPCache.

In your .user.ini file, add opcache.enable=0 into it. 

Once you've made the changes and saved the .user.ini file, PHP will pick up the new configuration the next time a PHP script is executed within the directory where the .user.ini file is located. OPCache will be disabled, and PHP will not utilize the OPCache extension.

However, some PHP applications may stop working due to it does not support opcache.enable=0. You should test your website after make this change.