Content · Programming · PHP

PHP apcu_add() Function: Usage and Examples

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

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

Adds a cache entry only if its key does not already exist.

What Is apcu_add() in PHP?

Adds a cache entry only if its key does not already exist.

Its syntax is:

apcu_add(string $key, mixed $value, int $ttl = 0): bool

Parameters:

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

Return type: bool. APCu must be installed and enabled. add() does not overwrite an existing item; CLI availability depends on apc.enable_cli.

Availability: Requires APCu enabled for the current PHP SAPI.

Example 1: Basic Usage

<?php

var_dump(apcu_add("guide:one", "Ready", 60));

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. APCu must be installed and enabled. add() does not overwrite an existing item; CLI availability depends on apc.enable_cli.

Example 2: A Different Input

<?php

var_dump(apcu_add("guide:one", "Replacement", 60));

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. APCu must be installed and enabled. add() does not overwrite an existing item; CLI availability depends on apc.enable_cli.

Practical Example: Applying It in a Program

<?php

$cacheKey = "settings:locale";
if (!apcu_add($cacheKey, "en_US", 300)) { echo "Value already cached"; }

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. APCu must be installed and enabled. add() does not overwrite an existing item; CLI availability depends on apc.enable_cli.

Important Behavior and Edge Cases

  • Key behavior: APCu must be installed and enabled. add() does not overwrite an existing item; CLI availability depends on apc.enable_cli.
  • Environment: Requires APCu enabled for the current PHP SAPI.

Comparison With Related Functions

FunctionPurpose
apcu_add()adds a cache entry only if its key does not already exist.
apcu_store()apcu_store() overwrites an existing cache entry; apcu_add() succeeds only when the key is absent.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: APCu must be installed and enabledAPCu must be installed and enabled. add() does not overwrite an existing item; CLI availability depends on apc.enable_cli.

Key Takeaways

  • adds a cache entry only if its key does not already exist.
  • APCu must be installed and enabled. add() does not overwrite an existing item; CLI availability depends on apc.enable_cli.
  • Requires APCu enabled for the current PHP SAPI.

Summary: APCu must be installed and enabled. add() does not overwrite an existing item; CLI availability depends on apc.enable_cli.

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