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
- What are potential security risks associated with using iframes in PHP for login functionality?
- What are the potential security risks of allowing all users to view the guest list on a website, and how can these risks be mitigated?
- What are the potential consequences of ignoring certain files, such as overall_header.tpl, in a PHP project?