Content · Programming · PHP

PHP apcu_dec() Function: Usage and Examples

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

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

Atomically decreases a cached integer.

What Is apcu_dec() in PHP?

Atomically decreases a cached integer.

Its syntax is:

apcu_dec(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. This only operates on compatible cached numeric values. Check the success flag rather than interpreting every return value as success.

Availability: Requires APCu enabled for the current PHP SAPI.

Example 1: Basic Usage

<?php

apcu_store("stock", 12);
echo apcu_dec("stock");

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. This only operates on compatible cached numeric values. Check the success flag rather than interpreting every return value as success.

Example 2: A Different Input

<?php

apcu_store("quota", 10);
echo apcu_dec("quota", 3);

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. This only operates on compatible cached numeric values. Check the success flag rather than interpreting every return value as success.

Practical Example: Applying It in a Program

<?php

apcu_store("remaining", 8);
$remaining = apcu_dec("remaining", 2, $operationSucceeded);
if ($operationSucceeded) { echo "Remaining: " . $remaining; }

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. This only operates on compatible cached numeric values. Check the success flag rather than interpreting every return value as success.

Important Behavior and Edge Cases

  • Key behavior: This only operates on compatible cached numeric values. Check the success flag rather than interpreting every return value as success.
  • Environment: Requires APCu enabled for the current PHP SAPI.

Comparison With Related Functions

FunctionPurpose
apcu_dec()atomically decreases a cached integer.
apcu_inc()apcu_inc() increases a numeric cache entry; apcu_dec() decreases it.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: This only operates on compatible cached numeric valuesThis only operates on compatible cached numeric values. Check the success flag rather than interpreting every return value as success.

Key Takeaways

  • atomically decreases a cached integer.
  • This only operates on compatible cached numeric values. Check the success flag rather than interpreting every return value as success.
  • Requires APCu enabled for the current PHP SAPI.

Summary: This only operates on compatible cached numeric values. Check the success flag rather than interpreting every return value as success.

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