How can passing parameters be a better practice than using global variables in PHP functions?

Passing parameters is a better practice than using global variables in PHP functions because it promotes encapsulation and reduces dependency on external variables. Global variables can lead to unexpected side effects and make the code harder to debug and maintain. By passing parameters, functions become more reusable and easier to test.

// Using parameters instead of global variables in a PHP function
function calculateTotal($price, $quantity) {
    return $price * $quantity;
}

$price = 10;
$quantity = 5;
$total = calculateTotal($price, $quantity);

echo "Total: $total";