What are some best practices for handling global variables in PHP to ensure consistent behavior and avoid unexpected results?

Global variables in PHP can lead to unexpected behavior and make code harder to maintain. To ensure consistent behavior and avoid unexpected results, it's best to limit the use of global variables and instead pass variables as parameters to functions or use object-oriented programming techniques. If global variables are necessary, consider using a configuration file to define and manage them centrally.

// Avoid using global variables and pass variables as parameters to functions
function calculateTotal($quantity, $price) {
    return $quantity * $price;
}

$total = calculateTotal(5, 10);
echo $total;