What are common reasons for undefined variable errors in PHP code?

Undefined variable errors in PHP code often occur when a variable is used without being initialized or declared beforehand. This can happen if a variable is misspelled, is out of scope, or has not been assigned a value. To solve this issue, make sure to properly initialize variables before using them in your code.

// Incorrect code causing undefined variable error
echo $name;

// Corrected code with variable initialization
$name = "John";
echo $name;