What are some best practices for calculating prices in a booking system using PHP?
When calculating prices in a booking system using PHP, it's important to consider factors such as base price, any additional fees or discounts, and the duration of the booking. One best practice is to create a function that takes these factors into account and returns the final price. This function should be reusable and easy to modify as pricing rules change.
function calculatePrice($basePrice, $duration, $additionalFees = 0, $discount = 0) {
$totalPrice = $basePrice * $duration + $additionalFees - $discount;
return $totalPrice;
}
// Example of calculating price for a booking
$basePrice = 100;
$duration = 3;
$additionalFees = 20;
$discount = 10;
$finalPrice = calculatePrice($basePrice, $duration, $additionalFees, $discount);
echo "The final price for the booking is: $" . $finalPrice;