What is the best practice for accessing and displaying prices from an array in PHP?

When accessing and displaying prices from an array in PHP, it is important to properly loop through the array and format the prices as needed before displaying them. One common approach is to use a foreach loop to iterate over the array and format each price using number_format() function to ensure consistent decimal places and thousand separators.

<?php
// Sample array of prices
$prices = [10.5, 20, 15.75, 30];

// Loop through the prices array and display formatted prices
foreach($prices as $price) {
    echo '$' . number_format($price, 2) . '<br>';
}
?>