What are some common pitfalls when dealing with variables and functions in PHP?

One common pitfall when dealing with variables and functions in PHP is variable scope. Variables declared inside a function are only accessible within that function unless they are explicitly declared as global. To avoid scope issues, it's best practice to pass variables as parameters to functions rather than relying on global variables.

// Incorrect usage of global variable
$globalVar = 10;

function printGlobalVar() {
    echo $globalVar; // This will throw an error
}

printGlobalVar();

// Correct usage by passing variable as parameter
$globalVar = 10;

function printGlobalVar($var) {
    echo $var;
}

printGlobalVar($globalVar);