← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Stacks

Data Structures PHP 5.3+ Beginner
debt(d7/e1/b3/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints indicate automated detection is 'no' and the only tool listed is Blackfire (a profiler). The common mistake of using array_shift instead of array_pop is a performance bug that produces correct output but degrades silently under load — only a profiler or careful code review would catch it. It won't surface as an error in normal testing.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix is explicit: replace array_shift with array_pop — a single-call swap. The fix is mechanical and localized to a single usage site.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The data structure choice is scoped to the algorithm or component that uses it. It applies across web/cli/queue contexts but the decision itself is local to wherever the stack is implemented — it doesn't impose a cross-codebase structural burden. Other parts of the codebase are unaffected.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception field identifies the core trap: confusing stacks (LIFO) with queues (FIFO), and the common_mistakes list confirms developers frequently use array_shift (queue behavior) when array_pop (stack behavior) is needed. This is a well-documented gotcha that competent developers eventually learn but routinely get wrong initially.

About DEBT scoring →

Also Known As

LIFO call stack SplStack push pop

TL;DR

A LIFO (Last In, First Out) data structure — the last element pushed is the first popped. Used for function call stacks, undo history, expression parsing, and DFS traversal.

Explanation

A stack supports three operations: push (add to top), pop (remove from top), peek (read top without removing). All are O(1). PHP's SplStack implements a doubly-linked-list-backed stack. The call stack is the most familiar stack — each function call pushes a frame, each return pops it. Stacks naturally model any 'reverse the order' or 'undo' problem. Bracket matching, postfix expression evaluation, and iterative DFS all use stacks explicitly.

Diagram

flowchart TD
    subgraph Stack_LIFO
        TOP[TOP] --> E3[Element C - last in]
        E3 --> E2[Element B]
        E2 --> E1[Element A - first in]
        E1 --> BOTTOM[BOTTOM]
    end
    PUSH[push C] -->|O of 1| TOP
    POP[pop] -->|O of 1 returns C| E3
    subgraph Applications
        CALLSTACK[Call stack<br/>function frames]
        UNDO[Undo history]
        DFS2[DFS traversal]
        BRACKETS[Bracket matching]
    end
style TOP fill:#238636,color:#fff
style E3 fill:#1f6feb,color:#fff

Common Misconception

Stacks and queues are interchangeable — stacks are LIFO (last in, first out); queues are FIFO (first in, first out). Reversing an array uses a stack; breadth-first search uses a queue.

Why It Matters

The PHP call stack is literally a stack — understanding stacks explains stack overflows from deep recursion and how backtracking algorithms work iteratively.

Common Mistakes

  • Implementing a stack with array_push/array_shift — shift is O(n); use array_push/array_pop (both O(1)).
  • Not checking isEmpty() before pop — popping an empty stack throws an exception in SplStack.
  • Using a queue where a stack is needed — the order of processing is reversed.
  • Confusing stack overflow (too many recursive calls) with heap overflow (memory exhaustion).

Code Examples

✗ Vulnerable
// Using array_shift as 'pop' — O(n) reindex:
$stack = [];
array_push($stack, 'a');
array_push($stack, 'b');
$top = array_shift($stack); // 'a' — wrong order AND O(n)
✓ Fixed
// array_pop is O(1) — correct LIFO:
$stack = [];
array_push($stack, 'a');
array_push($stack, 'b');
$top = array_pop($stack); // 'b' — correct LIFO, O(1)

// Or SplStack:
$stack = new SplStack();
$stack->push('a');
$stack->push('b');
$top = $stack->pop(); // 'b'

Added 16 Mar 2026
Edited 22 Mar 2026
Views 49
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 2 pings T 1 ping F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 1 ping T 2 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 9 Perplexity 7 Scrapy 5 Google 4 Ahrefs 4 Unknown AI 3 ChatGPT 3 SEMrush 3 Claude 2 Meta AI 1 Bing 1
crawler 35 crawler_json 6 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use PHP's array with array_push/array_pop as a stack (LIFO) — it's O(1) for both operations; use SplStack for explicit stack semantics with a more descriptive interface
📦 Applies To
PHP 5.3+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Array used as stack with array_shift instead of array_pop (O(n) vs O(1)); no explicit stack structure for DFS algorithm; call stack emulation in iterative algorithm
Auto-detectable: ✗ No blackfire
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Low Context: Function Tests: Update


✓ schema.org compliant