How can the sprintf function be used to format numbers in PHP?
The sprintf function in PHP can be used to format numbers by specifying the desired format within the function. This allows for control over the number of decimal places, padding, and alignment of the number. By using sprintf, you can easily format numbers in a specific way without having to manually manipulate the string representation of the number.
$num = 123.456;
$formatted_num = sprintf("%.2f", $num); // Formats the number with 2 decimal places
echo $formatted_num; // Output: 123.46