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

PHP References (&$var)

php PHP 5.0+ Intermediate
debt(d7/e3/b3/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). PHPStan and phpcs can flag some reference patterns but the classic foreach-by-reference leak and unintended mutation bugs typically slip past default configs and surface only in review or when downstream code misbehaves.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix is to drop the & and rely on copy-on-write, or add unset($item) after foreach — usually a small localised change, occasionally touching a few call sites if the signature changes.

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

Closest to 'localised tax' (b3). References apply within specific functions/loops (applies_to spans all PHP contexts but the choice is per-function), imposing a small ongoing tax on readers of that code rather than shaping the whole system.

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

Closest to 'serious trap' (t7). The misconception (references improve performance) contradicts PHP's copy-on-write reality, and the foreach `&$item` dangling reference is a notorious gotcha — the obvious mental model is wrong in multiple ways.

About DEBT scoring →

Also Known As

PHP references & operator pass by reference

TL;DR

References allow multiple variables to point to the same value — powerful but a frequent source of hard-to-debug side effects.

Explanation

In PHP, references (&) make two or more variables aliases of the same underlying value container. Assigning to one changes the other. References appear in: variable assignment ($b = &$a), function parameters (function foo(&$x)), foreach by reference (foreach ($arr as &$val)), and global declarations. Common pitfalls: a reference created in a foreach loop persists after the loop ends — always unset($val) afterwards to avoid modifying the last element unexpectedly. Overuse of references in large functions creates implicit coupling and makes code hard to reason about; prefer returning values.

Watch Out

After foreach ($arr as &$val) { ... }, always call unset($val) — the reference remains live and the next write to $val will silently overwrite the last array element.

Common Misconception

Passing large arrays by reference always improves performance. PHP uses copy-on-write — arrays are only copied when modified. Passing by reference prevents this optimisation and can cause subtle bugs if the function modifies the caller's variable unexpectedly.

Why It Matters

PHP passes values by copy by default — references (&) pass by pointer, meaning changes to the reference variable change the original. Misusing references causes subtle data mutation bugs.

Common Mistakes

  • foreach ($array as &$item) without unset($item) after the loop — the last element reference remains, corrupting the next use of $item.
  • Passing large arrays by reference for performance — PHP uses copy-on-write; references can actually be slower.
  • Not realising that assignment by reference ($b = &$a) makes both variables point to the same value.
  • Using references to return multiple values from a function — return an array or value object instead.

Code Examples

✗ Vulnerable
foreach ($items as &$item) { $item['price'] *= 1.1; }
// $item still references $items[last] — next assignment corrupts it
✓ Fixed
foreach ($items as &$item) { $item['price'] *= 1.1; }
unset($item); // break the reference

Added 15 Mar 2026
Edited 22 Mar 2026
Views 20
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 2 pings T 1 ping F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping 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 1 ping T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Amazonbot 9 Perplexity 3 Ahrefs 2 ChatGPT 2 Google 1
crawler 16 crawler_json 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Avoid passing large arrays by reference for performance — PHP uses copy-on-write so reading a large array is free; references only help when you need to modify the caller's variable
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
function(&$arr) for read-only large array parameter; reference used where return value would be cleaner
Auto-detectable: ✓ Yes phpstan phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Function Tests: Update

✓ schema.org compliant