Using OPcache

Zend OPcache works automatically once enabled and properly configured. You do not need to add anything to your PHP scripts, HTML pages, or application code.

OPcache operates transparently at the PHP engine level.

How it works

When PHP executes a script:

  1. PHP checks whether its compiled version is already in OPcache.
  2. If it is, PHP executes the cached opcodes.
  3. If it isn’t, PHP compiles the source and stores the resulting opcodes in shared memory.
  4. Subsequent requests reuse those compiled opcodes.

This happens automatically.

Configuration worth checking

In your PHP configuration (php.ini), these are the important settings:

opcache.enable=1
opcache.enable_cli=0
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=1
opcache.revalidate_freq=2

These are reasonable starting values, not mandatory settings. In particular:

With validate_timestamps=1 and revalidate_freq=2, changes to PHP files should generally be recognized within a couple of seconds.

If you disable timestamp validation for production performance, remember that you’ll need to reset OPcache or restart the PHP handler when deploying changed scripts.

How to verify OPcache is working

Create a temporary PHP file:

<?php
echo '<pre>';
print_r(opcache_get_status(false));
echo '</pre>';

Look for:

[opcache_enabled] => 1

You can also inspect:

These tell you whether scripts are being cached and how effectively OPcache is being used.

Security note: Don’t leave a publicly accessible diagnostic script on your website. It exposes server configuration information. Delete it after testing.

← All posts