In what scenarios would declaring a variable as "global" within a function be necessary in PHP programming?

Declaring a variable as "global" within a function in PHP is necessary when you want to access a variable that is defined outside of the function's scope. This allows you to use the variable within the function without having to pass it as a parameter. However, it is generally considered a bad practice as it can lead to code that is harder to maintain and debug.

$globalVar = 10;

function myFunction() {
    global $globalVar;
    echo $globalVar;
}

myFunction(); // Output: 10