How can global variables be used in PHP functions to ensure accessibility and avoid errors related to variable scope?

Global variables can be used in PHP functions by declaring them as global within the function. This ensures that the function can access and modify the global variable's value, even if it is defined outside of the function's scope. However, it is generally recommended to minimize the use of global variables to avoid potential conflicts and make the code more maintainable.

$globalVar = 10;

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

myFunction(); // Output: 10