How can database values be manipulated to ensure proper formatting when displaying prices in PHP?

When displaying prices in PHP, it is important to ensure proper formatting to maintain consistency and readability. One way to achieve this is by manipulating the database values before displaying them. This can be done by using PHP functions like number_format() to format the prices with the desired precision and decimal separator.

// Retrieve price from database
$price = $row['price'];

// Format price with 2 decimal places and comma as thousands separator
$formatted_price = number_format($price, 2, '.', ',');

// Display formatted price
echo '$' . $formatted_price;