How can global variables be effectively managed in PHP scripts to avoid conflicts and improve code quality?

Global variables can be effectively managed in PHP scripts by avoiding their excessive use and instead opting for more structured approaches like passing variables as function parameters or using classes and objects to encapsulate data. This helps prevent conflicts between different parts of the code and improves code quality by promoting better organization and reusability.

// Avoid using global variables and instead pass variables as function parameters
function calculateTotal($prices) {
    $total = 0;
    foreach ($prices as $price) {
        $total += $price;
    }
    return $total;
}

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