What is the significance of using global variables in PHP functions, and how can they impact variable scope?
Using global variables in PHP functions can impact variable scope by allowing the function to access and modify variables defined outside of its scope. This can be useful for sharing data between different parts of a program, but it can also lead to unexpected behavior and make code harder to maintain. It's generally recommended to avoid using global variables in favor of passing variables as function arguments or using return values to communicate data.
$globalVar = 10;
function modifyGlobalVar() {
global $globalVar;
$globalVar += 5;
}
modifyGlobalVar();
echo $globalVar; // Output: 15