What potential pitfalls are associated with the excessive use of global variables in PHP code?

Excessive use of global variables in PHP code can lead to code that is difficult to maintain, debug, and test. It can also introduce potential security vulnerabilities by making sensitive data easily accessible throughout the codebase. To mitigate these issues, it's recommended to limit the use of global variables and instead use more structured approaches like passing variables as function arguments or using classes and objects to encapsulate data.

// Avoid using global variables and pass variables as function arguments instead

function calculateTotal($items) {
    $total = 0;
    foreach ($items as $item) {
        $total += $item;
    }
    return $total;
}

$items = [10, 20, 30];
echo calculateTotal($items);