Content · Programming · PHP

PHP apcu_inc() Function: Usage and Examples

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

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

Atomically increments a cached integer.

What Is apcu_inc() in PHP?

Atomically increments a cached integer.

Its syntax is:

apcu_inc(string $key, int $step = 1, ?bool &$success = null, int $ttl = 0): int|false

Parameters:

  • $key — Accepted as string; required argument.
  • $step — Accepted as int; optional default 1.
  • $success — Accepted as ?bool &; optional default null.
  • $ttl — Accepted as int; optional default 0.

Return type: int|false. The increment is atomic, but creating a missing key requires explicit attention. Do not use process-local caches as durable financial counters.

Availability: Requires APCu enabled for the current PHP SAPI.

Example 1: Basic Usage

<?php

apcu_store("views", 5);
echo apcu_inc("views");

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. The increment is atomic, but creating a missing key requires explicit attention. Do not use process-local caches as durable financial counters.

Example 2: A Different Input

<?php

apcu_store("visits", 2);
echo apcu_inc("visits", 4);

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. The increment is atomic, but creating a missing key requires explicit attention. Do not use process-local caches as durable financial counters.

Practical Example: Applying It in a Program

<?php

$counterKey = "processed:jobs";
apcu_add($counterKey, 0, 3600);
$total = apcu_inc($counterKey, 1, $updated);
if ($updated) { echo "Processed: " . $total; }

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. The increment is atomic, but creating a missing key requires explicit attention. Do not use process-local caches as durable financial counters.

Important Behavior and Edge Cases

  • Key behavior: The increment is atomic, but creating a missing key requires explicit attention. Do not use process-local caches as durable financial counters.
  • Environment: Requires APCu enabled for the current PHP SAPI.

Comparison With Related Functions

FunctionPurpose
apcu_inc()atomically increments a cached integer.
apcu_dec()apcu_dec() decreases a cached integer while apcu_inc() increases it.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: The increment is atomic, but creating a missing key requires explicit attentionThe increment is atomic, but creating a missing key requires explicit attention. Do not use process-local caches as durable financial counters.

Key Takeaways

  • atomically increments a cached integer.
  • The increment is atomic, but creating a missing key requires explicit attention. Do not use process-local caches as durable financial counters.
  • Requires APCu enabled for the current PHP SAPI.

Summary: The increment is atomic, but creating a missing key requires explicit attention. Do not use process-local caches as durable financial counters.

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