Content · Programming · PHP

PHP apcu_store() Function: Usage and Examples

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

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

Writes or replaces cached values in APCu.

What Is apcu_store() in PHP?

Writes or replaces cached values in APCu.

Its syntax is:

apcu_store(string|array $key, mixed $value = null, int $ttl = 0): mixed

Parameters:

  • $key — Accepted as string|array; required argument.
  • $value — Accepted as mixed; optional default null.
  • $ttl — Accepted as int; optional default 0.

Return type: mixed. A time-to-live of 0 means no explicit expiration, but cache eviction and server restarts can still remove values.

Availability: Requires APCu enabled for the current PHP SAPI.

Example 1: Basic Usage

<?php

var_dump(apcu_store("site:title", "Know-Stuff", 60));

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. A time-to-live of 0 means no explicit expiration, but cache eviction and server restarts can still remove values.

Example 2: A Different Input

<?php

var_dump(apcu_store("site:enabled", true, 120));

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. A time-to-live of 0 means no explicit expiration, but cache eviction and server restarts can still remove values.

Practical Example: Applying It in a Program

<?php

$cacheKey = "catalog:summary";
$summary = ["items" => 12, "updated" => "today"];
if (apcu_store($cacheKey, $summary, 300)) { echo "Summary cached"; }

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. A time-to-live of 0 means no explicit expiration, but cache eviction and server restarts can still remove values.

Important Behavior and Edge Cases

  • Key behavior: A time-to-live of 0 means no explicit expiration, but cache eviction and server restarts can still remove values.
  • Environment: Requires APCu enabled for the current PHP SAPI.

Comparison With Related Functions

FunctionPurpose
apcu_store()writes or replaces cached values in APCu.
apcu_add()apcu_add() avoids overwriting an existing key; apcu_store() inserts or updates it.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: A time-to-live of 0 means no explicit expiration, but cache eviction and server restarts can still remove valuesA time-to-live of 0 means no explicit expiration, but cache eviction and server restarts can still remove values.

Key Takeaways

  • writes or replaces cached values in APCu.
  • A time-to-live of 0 means no explicit expiration, but cache eviction and server restarts can still remove values.
  • Requires APCu enabled for the current PHP SAPI.

Summary: A time-to-live of 0 means no explicit expiration, but cache eviction and server restarts can still remove values.

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