How to determine if PHP OPcache is enabled or not?
A common point of confusion when managing PHP performance involves interpreting configuration files versus runtime behavior. You might inspect your php.ini and find that every line related to the extension is commented out, yet running phpinfo() displays a massive section dedicated to OPcache. This discrepancy often leaves developers asking whether the cache is actually active or if they are misinterpreting the output.
The answer lies in understanding the distinction between the configuration file state and the loaded module state. When you look at a php.ini file, you are seeing the defaults or static settings defined for that specific environment. If lines like opcache.enable=0 are commented out, it does not necessarily mean the extension is disabled; it often means the setting has reverted to its built-in default value within the PHP core or an included .dist file.
In many standard PHP distributions, including versions around 5.5 and later, OPcache is compiled into the Zend engine as a core feature rather than just a loadable module in some configurations. Consequently, the extension loads automatically upon starting the PHP interpreter, regardless of what you see in the text-based configuration file. The presence of an "OPcache" section in phpinfo() is the definitive proof that the memory manager is active and ready to store precompiled script bytecode.
To verify this programmatically without relying on visual inspection of HTML output, you should query the runtime status directly. You can create a simple PHP script containing just one line:
<?php
var_dump(ocache_get_status());
?>
If the extension is loaded and enabled, this command will return an array containing data about the current cache state, such as the number of hits, misses, and the memory usage. If the extension were truly disabled or unloaded, this function would throw a fatal error stating that the function ocache_get_status() does not exist.
Furthermore, you can check the specific configuration directives at runtime using ini_get(). Even if your php.ini shows commented-out lines, running the following code will reveal the actual active value:
<?php
echo "OPcache enabled: " . (ini_get('opcache.enable') ? 'Yes' : 'No');
echo "\nMemory limit: " . ini_get('opcache.memory_consumption');
?>
This approach is superior to checking the file because it bypasses any ambiguity regarding whether a directive was explicitly set to 0 or left as a default. It confirms the state of the system exactly as PHP sees it in memory.
It is also worth noting that Apache modules and CLI versions can sometimes have separate configurations. In your specific setup with Ubuntu 12.04 and Apache 2.4, you must ensure that the opcache extension is listed in the module list for both the web server context (usually managed by apache2.conf or a specific load file) and the CLI context if you run PHP scripts from the command line. However, seeing the output in phpinfo() confirms at least one of these contexts is active.
If your goal is to disable OPcache because it is conflicting with another caching layer or causing memory issues, commenting out the lines in php.ini is not enough on its own if the extension was compiled as core. You would need to explicitly set opcache.enable=0, restart Apache (service apache2 restart), and then verify again that the phpinfo() output no longer contains the OPcache section. Understanding this separation between static configuration text and dynamic runtime state is crucial for effective PHP optimization.
