How does the scope of variables differ when using "require" within a function in PHP?

When using "require" within a function in PHP, the scope of variables declared outside the function remains unchanged. This means that variables declared outside the function are accessible within the function, but variables declared within the function are not accessible outside of it. To ensure that variables declared within the function are accessible outside of it, you can use the "global" keyword to explicitly declare the variable as global within the function.

$globalVar = "I am a global variable";

function myFunction() {
    global $globalVar;
    $localVar = "I am a local variable";
    
    echo $globalVar; // Output: I am a global variable
    echo $localVar; // Output: I am a local variable
}

myFunction();

echo $globalVar; // Output: I am a global variable
echo $localVar; // This will throw an error as $localVar is not accessible outside the function