How can I maintain leading zeros in a PHP loop?

When working with numbers in PHP, leading zeros may be removed if the number is treated as an integer rather than a string. To maintain leading zeros in a PHP loop, you can use the str_pad() function to add leading zeros to the number before outputting it.

// Loop from 1 to 10 with leading zeros maintained
for ($i = 1; $i <= 10; $i++) {
    $number = str_pad($i, 2, '0', STR_PAD_LEFT); // Add leading zeros to the number
    echo $number . "<br>";
}