How can the use of global variables be avoided in PHP functions for better code organization?

Global variables can lead to code that is difficult to maintain and debug, as they can be accessed and modified from anywhere in the codebase. To avoid using global variables in PHP functions, you can pass variables as parameters to the function or use object-oriented programming techniques such as encapsulation and dependency injection.

// Avoid using global variables by passing variables as parameters to the function
function calculateTotal($price, $quantity) {
    return $price * $quantity;
}

$price = 10;
$quantity = 5;
$total = calculateTotal($price, $quantity);
echo "Total: $total";