What are the potential pitfalls of using global variables in PHP scripts?
Using global variables in PHP scripts can lead to several potential pitfalls, such as making code harder to debug and maintain, increasing the risk of naming conflicts, and making the code less reusable. To avoid these issues, it is recommended to use more structured and organized approaches like passing variables as function parameters or using classes and objects to encapsulate data.
// Avoid using global variables by passing variables as function parameters
function calculateTotal($price, $quantity) {
return $price * $quantity;
}
$total = calculateTotal(10, 5);
echo $total;