What are the potential pitfalls of not using global variables in PHP functions?
Potential pitfalls of not using global variables in PHP functions include limited scope, which can lead to confusion and errors when trying to access variables outside of the function. To avoid this, you can pass variables as arguments to the function instead of relying on global scope.
// Avoid using global variables in PHP functions by passing variables as arguments
$globalVar = 10;
function doSomething($var) {
return $var * 2;
}
$result = doSomething($globalVar);
echo $result; // Output: 20