Content · Web Development · PHP

PHP opcache_is_script_cached() Function: Usage and Examples

Checks whether a script path is currently present in the OPcache of the active SAPI.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Admin

Checks whether a script path is currently present in the OPcache of the active SAPI.

What It Does and When to Use It


Ask whether a specific resolved path is cached in the active SAPI. OPcache caches compiled opcodes, JIT may optimize CPU-intensive PHP, while APCu caches application-owned values.

Requirements, Inputs and Expected Behavior


Confirm the runtime, extension, environment variables, and permissions before running the examples in production. CLI and FPM caches are separate; path and cache-warming timing affect results.

Example 1: Basic Operation


Ask whether a specific resolved path is cached in the active SAPI.

<?php
$file = realpath(__FILE__);
 if ($file !== false) var_dump(opcache_is_script_cached($file));

Example 2: Behavior and Validation


Check the effective configuration when a file is not cached.

<?php
$settings = opcache_get_configuration()['directives'] ?? [];
 echo 'OPcache: ', (int)($settings['opcache.enable'] ?? 0);

Practical Example: Application Use


Compare cached status for the current file and another application script.

<?php
foreach ([__FILE__, __DIR__ . '/bootstrap.php'] as $candidate) {
     $resolved = realpath($candidate);
     echo basename($candidate), ': ', ($resolved && opcache_is_script_cached($resolved) ? 'cached' : 'not cached'), "
";
 }

Important Behavior and Edge Cases


The second example highlights: Check the effective configuration when a file is not cached. The practical example adds: Compare cached status for the current file and another application script.

Comparison with Related Approaches


ApproachWhat changes
OPcache, JIT and APCuOPcache caches compiled opcodes, JIT may optimize CPU-intensive PHP, while APCu caches application-owned values.
Choosing safelyProfile application response times and memory under representative traffic before tuning.

Common Beginner Mistakes


MistakeCorrect approach
Assuming a cache is shared across multiple serversConfirm process- and host-level cache scope; use a distributed store when needed.
Never invalidating cached dataSet a TTL or versioned cache key and test release-time invalidation.
Skipping operational constraintsCLI and FPM caches are separate; path and cache-warming timing affect results.

Key Takeaways


  • Checks whether a script path is currently present in the OPcache of the active SAPI.
  • Compare cached status for the current file and another application script.
  • Profile application response times and memory under representative traffic before tuning.
Share this resourceSend this page to someone who may find it useful.