How can global variables in PHP impact the functionality of scripts and cause unexpected behavior?

Global variables in PHP can impact the functionality of scripts by introducing unexpected behavior due to their scope and accessibility. To avoid these issues, it's recommended to limit the use of global variables and instead pass necessary data as parameters to functions or use constants for values that should not change.

// Avoid using global variables and pass necessary data as parameters
function calculateSum($num1, $num2) {
    return $num1 + $num2;
}

$num1 = 5;
$num2 = 10;
echo calculateSum($num1, $num2);