Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 480453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:53:40+00:00 2026-05-13T00:53:40+00:00

I am looping an array of one-digit and two-digit numbers. When printing these values,

  • 0

I am looping an array of one-digit and two-digit numbers.

When printing these values, I need to ensure that all values are shown as two-digit numbers.

I need a solution to prepend zeros to the single-digit numbers but leave the two-digit numbers unchanged.

In other words, I want to "left pad" a numeric string to a minimum of two digits by adding zeros.

How can I change my code to render leading 0’s for values 1 through 9?

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

Expected result:

<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:53:40+00:00Added an answer on May 13, 2026 at 12:53 am
    <?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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to push elements from one array into respective rows of another array.
Is there an XMLList equivalent to Array.indexOf? For example - var array:Array = ['one','two'];
I am looping through an array, and for each value, I need to insert
I am looping through an array of urls. Each one fetches some jsonp data
I'm looking for way to change all array's keys to one key for all
I'm looking for a kind of array data-type that can easily have items added,
I'm interested in speed, not good looking code, that is why I'm using array
I want to smoothly transition between two audio clips that are loaded into an
Looping through a dynamically-named array and comparing results to the previous positions results. if($s>1
All right, so I am being passed an array of checked items represented by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.