How can PHP be used to format numbers with a specific number of decimal places for display in HTML?

To format numbers with a specific number of decimal places for display in HTML using PHP, you can use the number_format() function. This function allows you to specify the number of decimal places you want to display. Simply pass the number you want to format as the first parameter, the number of decimal places as the second parameter, and any additional parameters for thousands separator and decimal point if needed.

<?php
$number = 123.456789;
$decimal_places = 2;
$formatted_number = number_format($number, $decimal_places);
echo $formatted_number;
?>