How can the price be formatted with commas in PHP when retrieved from a database as a decimal type?
When retrieving a price from a database as a decimal type in PHP, you can format it with commas to make it more readable by using the number_format() function. This function allows you to specify the number of decimal places, the decimal point, and the thousands separator. By using number_format() on the retrieved price, you can easily display it with commas.
// Retrieve price from database
$price = 12345.67;
// Format price with commas
$formatted_price = number_format($price, 2, '.', ',');
// Output formatted price
echo $formatted_price;