Q:

PHP adding leading 0 to range

How do I have it render leading 0's for 1-9 ?
<?php foreach (range(1, 12) as $month): ?>

                  <option value="<?=$month?>"><?=$month?></option>

                <?php endforeach?>

A:

<?php foreach (range(1, 12) as $month): ?>
  <option value="<?= sprintf("%02d", $month) ?>"><?= sprintf("%02d", $month) ?></option>
<?php endforeach?>

You'd probably want to save the value of sprintf to a variable to avoid calling it multiple times.

A:

Use either str_pad():
echo str_pad($month, 2, '0', STR_PAD_LEFT);

or sprintf():
echo sprintf('%02d', $month);

