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

Stacks

data_structures PHP 5.3+ Beginner

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 26
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
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 2 pings F 0 pings S 0 pings S 1 ping M 1 ping T 0 pings W 1 ping T 2 pings F 1 ping 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 1 ping M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 8 Perplexity 7 Google 4 Unknown AI 3 Ahrefs 1 ChatGPT 1 SEMrush 1
crawler 21 crawler_json 3 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