How can the str_pad function in PHP be used to ensure a consistent two-digit output for numbers in loops?

When looping through numbers in PHP, it's common to encounter single-digit numbers that need to be formatted as two-digit numbers for consistency. The str_pad function in PHP can be used to add leading zeros to these numbers to ensure a consistent two-digit output. By specifying the total length as 2 and the padding character as '0', we can easily achieve this formatting.

for ($i = 1; $i <= 12; $i++) {
    $formatted_number = str_pad($i, 2, '0', STR_PAD_LEFT);
    echo $formatted_number . "\n";
}