How can global variables be accessed within a function in PHP?

Global variables can be accessed within a function in PHP by using the global keyword followed by the variable name within the function. This allows the function to access and modify the global variable's value. However, it is generally considered a bad practice to rely heavily on global variables as it can lead to code that is difficult to maintain and debug.

$globalVar = 10;

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

accessGlobalVariable(); // Output: 10