"Undefined variable $x" in PHP — What It Means and How to Fix It
What the PHP error "Undefined variable $x" means and how to fix it.
Published October 30, 2026
"Undefined variable $x" occurs when code reads a variable that was never assigned a value in the current scope.
Key points
- Check the exact file and line number PHP reports — the mistake is often on or just before that line
Example
echo $name; // Warning: Undefined variable $name