What are common pitfalls when calculating prices with PHP, especially regarding rounding errors?

When calculating prices with PHP, a common pitfall is encountering rounding errors due to the inherent limitations of floating-point arithmetic. To avoid these errors, it is recommended to perform all calculations using integer values representing the smallest unit of currency (e.g., cents) and only convert back to a decimal format for display purposes.

// Calculate price in cents to avoid rounding errors
$price = 10.99; // price in dollars
$priceInCents = round($price * 100); // convert to cents

// Perform calculations using integer values
$taxRate = 0.0825; // 8.25%
$taxInCents = round($priceInCents * $taxRate);
$totalInCents = $priceInCents + $taxInCents;

// Convert total back to decimal format for display
$total = number_format($totalInCents / 100, 2);

echo "Total price: $" . $total;