If you're working on optimizing the performance of the PHP application, OPcache is a great tool to have enabled. OPcache improves the performance of PHP by storing precompiled script bytecode in memory, eliminating the need for PHP to load and parse scripts on each request. This tutorial demonstrates how to check if OPcache is enabled in PHP.
Below is a simple PHP script to determine whether OPcache is enabled on the server:
<?php
function isOpcacheEnabled(): bool
{
if (!function_exists('opcache_get_status')) {
return false;
}
return !empty(opcache_get_status()['opcache_enabled']);
}
echo isOpcacheEnabled() ? 'Enabled' : 'Disabled';
This PHP code defines a function that verifies the existence of the opcache_get_status
function, as its absence indicates that OPcache is not installed or supported. If the function is available, it checks the opcache_enabled
key from the OPcache status to determine whether OPcache is active. Based on the result, the script outputs either "Enabled" or "Disabled".
Leave a Comment
Cancel reply