Skip to content
Related Articles

Related Articles

How to use PHP OPCache ?

Improve Article
Save Article
  • Last Updated : 31 Dec, 2021
Improve Article
Save Article

The OPCache is used for improving the performance of PHP as it stores the precompiled bytecode, in result deleting the need for loading and parsing the PHP scripts upon each request.

Requirements: Packages such as Zend OPCache are required for the purposeful use. The zendOPCache package contains PHP versions 5.2, 5.3 and 5.4. This package is used with the basic fulfillment of the need for opcode caching and hence optimization. It will improve the performance of PHP by storing the precompiled bytecode in the shared memory and eliminating the need of reading the code from the disk and compiling it for future access.
For the Package download of Zend OPCache(Direct Download Links):

VersionRelease StateLink
7.0.1betaZend OPCacheV 7.0.1(83.1kB)
7.0.2betaZend OPCache V 7.0.2(85.8kB)
7.0.3betaZend OPCache V 7.0.3(92.0kB)
7.0.4StableZend OPCache V 7.0.4(94.1kB)
7.0.5StableZend OPCache V 7.0.5(94.8kB)

Enabling OPCache extensions:

  • For PHP Versions 5.2, 5.3 and 5.4

    Due to Unavailability of a DLL(Dynamic Link Library) for PECL(PHP Extension and Application Repository) installation of the PECL extensions can be found here.

  • For PHP Versions 5.5.0 or later
    OPCache can only be compiled as a shared extension under this version. Firstly, you need to enable the building of default extension with –enable-opcache option to make it available.
    Afterwards, you can use the zend_extension configuration directive to lead the OP Cache extension into PHP. Use zend_extension=/full/path/to/ on non-Windows platforms, and zend_extension=C:\path\to\php_opcache.dll on Windows.

    To use OPCache with Xdebug, you need to load OPCache before Xdebug.

  • Information regarding New Releases, Downloads, ChangeLog and additional information can be found here.

Advised php.ini setting:

  • Make the following changes in php.ini file for optimized performance.

A full list of configuration directive supported by OPcache is also available.

OPCache Functions:

  1. opcache_compile_file() Function: This function is used to compile and cache a PHP script without executing it.


    bool opcache_compile_file( $file ) 

    This function compiles a PHP script and adds that in the opcode cache without executing the file, this can be used to prime the cache after a Web server restart by pre-caching files that will be included later requests. The $file is used as a parameter. It is the path to the PHP script to be compiled. The above description returns true if the file was compiled successfully else false.

    Errors/Exceptions: For any errors (if occur), for example, in this case, an error of level E_Warning if generated, use as a @ as a prefix, as we prefix it, an error message that might be generated might get ignored.
    If any custom error Handler function with set_error_handler() is called, which in turn calls the error_reporting() which returns 0 when preceded by a @ which means, that error is ignored and the program proceeds further.
    If the track_errors feature is enabled, any error message generated by the expression will be saved in a variable namely $php_errormsg which overwrites any further errors if occur. Also, this variable can be checked early if you want to make use of it.


    // Intentional file error
    $my_file = @file('non existent file'
    or die("Failed opening file, error was : '$php_errormsg'" );
    // This works for any expression, not just functions
    $value = @$cache[$key];

    Note: This operator is known by the veteran PHPers as the STFU operator.

  2. opcache_get_configuration() Function: This function is used to get the configuration information about the cache.


    array opcache_get_configuration( void )

    This function returns configuration data about the cache instance and also returns an array of information including the ini file.

    Errors/Exceptions: If opcache.restrict_api is in use and the current path is in violation of the rule then an E_WARNING will be raised, no status information will be returned.

  3. opcache_get_status() Function: This function is used to get the status information about the cache.


    array opcache_get_status( $get_scripts = TRUE )

    This function returns the state information about the cache instance, $get_scripts is used as a parameter including script specific state information.

    Return Value: It returns an array of information, and it will optionally containing script specific state information, or FALSE on failure.

    Errors/Exceptions: If opcache.restrict_api is in use and the current path is in violation of the rule then an E_WARNING will be raised, no status information will be returned.

  4. opcache_invalidate() Function: This function is used to invalidate the cached script.


    bool opcache_invalidate( $script, $force = FALSE )

    This function invalidates the particular script from the opcode cache. If force is unset or FALSE, the script will only be invalidated if the modification time of the script is new compared to the cached opcodes. The $script is used as a parameter denoting the path to the script being invalidated. The $force is used as a parameter if it set to TRUE, the script will be invalidated regardless of whether invalidation is necessary.

    Return value: TRUE if the opcode cache for the script was invalidated or if there was nothing to invalidate, or FALSE if the opcode cache is disabled.

  5. opcache_is_script_cached() Function: It will tell whether a script is cached in OPCache or not.


    bool opcache_is_script_cached( $file )

    This function checks if a PHP script has been cached in OPCache. This could be used more easily to detect the “warning” of the cache for the particular script. The $file is used as a parameter describes the path to the PHP script being checked.

    Returns: It will returns TRUE if file is cached in OPCache, FALSE otherwise.

  6. opcache_reset() Function: This function resets the contents of the opcode cache.


    bool opcache_reset( void ) 

    This function resets the entire opcode cache. After calling opcache_reset(), all scripts will be reloaded and reparsed the next time they are hit.

    Return Value: It will return TRUE if the opcode cache was reset and FALSE if the opcode cache is disabled.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!