What are the potential pitfalls of using global variables in PHP scripts, and how can they be avoided?
Using global variables in PHP scripts can lead to issues such as variable scope conflicts, making code harder to debug and maintain. To avoid these pitfalls, it's recommended to limit the use of global variables and instead use function parameters or return values to pass data between functions.
// Avoid using global variables by passing data as function parameters
function calculateTotal($price, $quantity) {
return $price * $quantity;
}
$total = calculateTotal(10, 5);
echo $total;