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

list() / Short Array Destructuring

PHP PHP 5.5+ Beginner
debt(d5/e1/b1/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list Rector and php-cs-fixer, both specialist/automated tools that can identify the repetitive $arr[0]/$arr[1] pattern and flag or transform it. A standard linter won't catch this by default, but these dedicated tools will.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix explicitly describes replacing repetitive index access with list() or [] syntax — a single-expression swap. No structural changes required.

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

Closest to 'minimal commitment' (b1). This is a local syntax choice at the assignment site. It touches a single line or expression and imposes no ongoing tax on other parts of the codebase. The tags confirm it is purely a syntax/arrays concern.

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

Closest to 'notable trap' (t5). The misconception field identifies that list() and [] are not fully interchangeable — list() preserves historical left-to-right assignment order in some edge cases. Additionally, common_mistakes include silently using positional destructuring on associative arrays and missing length checks causing undefined index notices. These are documented gotchas that developers eventually learn, consistent with t5.

About DEBT scoring →

Also Known As

list() PHP list assignment array destructuring list

TL;DR

Assigns array elements to variables in a single expression — list($a, $b) or the shorthand [$a, $b] = $array.

Explanation

PHP's list() construct (and its [] shorthand since PHP 7.1) destructures arrays into named variables. PHP 7.1 enabled key-based destructuring (['name' => $name, 'age' => $age] = $row) for associative arrays, greatly improving database row handling. Nested destructuring is supported. list() is especially useful with foreach for unpacking tuple-like arrays: foreach ($rows as [$id, $name]) and for swapping variables: [$a, $b] = [$b, $a]. Accessing a non-existent key emits a notice — validate structure first.

Common Misconception

list() and [] destructuring are fully interchangeable. They behave identically in PHP 7.1+ for most cases, but list() preserves historical left-to-right assignment order in some edge cases. The [] syntax is preferred in modern PHP for brevity and consistency.

Why It Matters

list() and its short form [] allow assignment of multiple variables from an array in one expression — commonly used with preg_match, explode, and database row destructuring.

Common Mistakes

  • Using list() with associative arrays without specifying keys — positional destructuring of associative arrays is error-prone.
  • Not checking that the source array has enough elements — undefined index notices on short arrays.
  • Confusing list() (assignment) with array() (creation) — similar syntax, completely different purpose.
  • Using list() in PHP < 7.1 for associative array destructuring — key-based list() was added in 7.1.

Code Examples

✗ Vulnerable
// Positional list() on associative array — order-dependent and fragile:
[$name, $email] = $user; // Relies on array key order — breaks if order changes

// Safe:
['name' => $name, 'email' => $email] = $user; // Explicit key mapping
✓ Fixed
// list() / [] — destructure arrays into variables
[$first, $second, $third] = [10, 20, 30];

// Skip elements
[, $second] = getCoordinates(); // ignore x, capture y

// Nested
[[$x1, $y1], [$x2, $y2]] = [[1,2],[3,4]];

// Associative (PHP 7.1+)
['name' => $name, 'age' => $age] = $user;

// In foreach — unpack each row
$points = [[1,2,'A'], [3,4,'B']];
foreach ($points as [$x, $y, $label]) {
    echo "$label: ($x, $y)\n";
}

// Classic swap without temp variable
[$a, $b] = [$b, $a];

Added 15 Mar 2026
Edited 22 Mar 2026
Views 53
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 1 ping S 0 pings S 1 ping M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 3 pings S 1 ping M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Amazonbot 7 Perplexity 6 SEMrush 5 Google 5 Ahrefs 4 Scrapy 4 ChatGPT 3 Unknown AI 3 PetalBot 2 Majestic 1 Claude 1 Meta AI 1 Bing 1 Sogou 1
crawler 38 crawler_json 5 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use list() or the short [] syntax to unpack arrays into named variables — it makes intent clear and avoids repetitive $arr[0], $arr[1] index access
📦 Applies To
PHP 5.5+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
$a=$arr[0]; $b=$arr[1]; $c=$arr[2]; pattern that list() or [$a,$b,$c]=$arr would express better
Auto-detectable: ✓ Yes rector php-cs-fixer
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✓ Auto-fixable Fix: Low Context: Line


✓ schema.org compliant