How can the provided PHP code be rewritten to function correctly?
The issue with the provided PHP code is that the variable `$total` is being used within the `calculateTotalPrice` function without being passed as an argument. To solve this, we need to pass `$total` as an argument to the function.
function calculateTotalPrice($price, $quantity, $total) {
$total += $price * $quantity;
return $total;
}
$total = 0;
$total = calculateTotalPrice(10, 2, $total);
echo "Total price: $" . $total;
Related Questions
- How can header information be modified without causing errors in PHP scripts?
- What are some best practices for naming PHP files that contain XML content to avoid browser parsing issues?
- When using variables in SQL queries in PHP, what are the recommended methods for proper formatting and concatenation to avoid syntax errors?