What potential issues can arise when using the $GLOBALS array in PHP scripts, and how can they be avoided?
Using the $GLOBALS array in PHP scripts can lead to potential security vulnerabilities such as global variable contamination and unintended variable overwriting. To avoid these issues, it is recommended to limit the usage of $GLOBALS and instead use more localized variable scopes such as function parameters or class properties.
// Avoid using $GLOBALS and use function parameters or class properties instead
// Example of avoiding $GLOBALS by passing variables as function parameters
function calculateSum($num1, $num2) {
return $num1 + $num2;
}
$number1 = 10;
$number2 = 20;
$sum = calculateSum($number1, $number2);
echo $sum;