What is the purpose of using number_format in PHP and how can it be applied to format numerical values?

The purpose of using number_format in PHP is to format numerical values by adding thousand separators and specifying the number of decimal places. This function is useful for displaying numbers in a more readable format, especially when dealing with currency or large numbers. It can be applied by passing the number to be formatted as the first parameter, the number of decimal places as the second parameter, the decimal separator as the third parameter, and the thousand separator as the fourth parameter.

$number = 1234567.89;
$formatted_number = number_format($number, 2, '.', ',');
echo $formatted_number; // Output: 1,234,567.89