Are there alternative methods to using $GLOBALS in PHP programming?

Using $GLOBALS in PHP can lead to global variable pollution and make code harder to maintain and debug. It is recommended to avoid using $GLOBALS and instead use more structured approaches like passing variables as function parameters or using classes and objects to encapsulate data. This helps improve code readability, reusability, and maintainability.

// Avoid using $GLOBALS by passing variables as function parameters

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

$items = [10, 20, 30];
$total = calculateTotal($items);
echo "Total: $total";