How to use PHP OPCache ? - GeeksforGeeks

31 Dec, 2021
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):
Enabling OPCache extensions:
For PHP Versions 5.2, 5.3 and 5.4Due to Unavailability of a DLL(Dynamic Link Library) for PECL(PHP Extension and Application Repository) installation of the PECL extensions can be found here.
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 laterOPCache 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/opcache.so 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.
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.opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
A full list of configuration directive supported by OPcache is also available.
OPCache Functions:
opcache_compile_file() Function: This function is used to compile and cache a PHP script without executing it.Syntax: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.phpphp<?php  // 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.opcache_get_configuration() Function: This function is used to get the configuration information about the cache.Syntax: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.opcache_get_status() Function: This function is used to get the status information about the cache.Syntax: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.opcache_invalidate() Function: This function is used to invalidate the cached script.Syntax: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.opcache_is_script_cached() Function: It will tell whether a script is cached in OPCache or not.Syntax: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.opcache_reset() Function: This function resets the contents of the opcode cache.Syntax: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.
opcache_compile_file() Function: This function is used to compile and cache a PHP script without executing it.Syntax: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.phpphp<?php  // 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.
Syntax:
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.
php
<?php  // 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.
opcache_get_configuration() Function: This function is used to get the configuration information about the cache.Syntax: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.
Syntax:
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.
opcache_get_status() Function: This function is used to get the status information about the cache.Syntax: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.
Syntax:
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.
opcache_invalidate() Function: This function is used to invalidate the cached script.Syntax: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.
Syntax:
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.
opcache_is_script_cached() Function: It will tell whether a script is cached in OPCache or not.Syntax: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.
Syntax:
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.
opcache_reset() Function: This function resets the contents of the opcode cache.Syntax: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.
Syntax:
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.
sagar0719kumar
simmytarika5
arorakashish0911
Picked
PHP
PHP Programs
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to fetch data from localserver database and display on HTML table using PHP ?
How to pass form variables from one page to other page in PHP ?
Create a drop-down list that options fetched from a MySQL database in PHP
How to create admin login page using PHP?
Different ways for passing data to view in Laravel
How to call PHP function on the click of a Button ?
How to fetch data from localserver database and display on HTML table using PHP ?
How to pass form variables from one page to other page in PHP ?
How to create admin login page using PHP?
How to Install php-curl in Ubuntu ?