Content · Programming · PHP

PHP apcu_entry() Function: Usage and Examples

Learn how PHP apcu_entry works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Returns a cached value or computes and caches it with a callback.

What Is apcu_entry() in PHP?

Returns a cached value or computes and caches it with a callback.

Its syntax is:

apcu_entry(string $key, callable $callback, int $ttl = 0): mixed

Parameters:

  • $key — Accepted as string; required argument.
  • $callback — Accepted as callable; required argument.
  • $ttl — Accepted as int; optional default 0.

Return type: mixed. The callback is run only when the cache needs a value. Avoid lengthy callbacks and do not assume the callback executes on each request.

Availability: Requires APCu enabled for the current PHP SAPI.

Example 1: Basic Usage

<?php

$result = apcu_entry("answer", fn() => 42, 60);
echo $result;

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. The callback is run only when the cache needs a value. Avoid lengthy callbacks and do not assume the callback executes on each request.

Example 2: A Different Input

<?php

$result = apcu_entry("greeting", fn() => strtoupper("hello"), 60);
echo $result;

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. The callback is run only when the cache needs a value. Avoid lengthy callbacks and do not assume the callback executes on each request.

Practical Example: Applying It in a Program

<?php

$cacheKey = "site:menu";
$menu = apcu_entry($cacheKey, function () { return ["Home", "Guides", "About"]; }, 120);
print_r($menu);

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. The callback is run only when the cache needs a value. Avoid lengthy callbacks and do not assume the callback executes on each request.

Important Behavior and Edge Cases

  • Key behavior: The callback is run only when the cache needs a value. Avoid lengthy callbacks and do not assume the callback executes on each request.
  • Environment: Requires APCu enabled for the current PHP SAPI.

Comparison With Related Functions

FunctionPurpose
apcu_entry()returns a cached value or computes and caches it with a callback.
apcu_fetch()apcu_fetch() reads an existing value; apcu_entry() can compute and cache a missing value using a callback.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: The callback is run only when the cache needs a valueThe callback is run only when the cache needs a value. Avoid lengthy callbacks and do not assume the callback executes on each request.

Key Takeaways

  • returns a cached value or computes and caches it with a callback.
  • The callback is run only when the cache needs a value. Avoid lengthy callbacks and do not assume the callback executes on each request.
  • Requires APCu enabled for the current PHP SAPI.

Summary: The callback is run only when the cache needs a value. Avoid lengthy callbacks and do not assume the callback executes on each request.

Share this resourceSend this page to someone who may find it useful.