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

  • SEARCH
  • Home
  • 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 615203
In Process

The Archive Base Latest Questions

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

for ($number = 1; $number <= 16; $number++) { echo $number . \n; }

  • 0
for ($number = 1; $number <= 16; $number++) {
    echo $number . "\n";
}

This code outputs:

1
2
3
...
16

How can I get PHP to output the numbers preceded by zeros?

01
02
03
...
16
  • 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-13T18:11:41+00:00Added an answer on May 13, 2026 at 6:11 pm

    You could use sprintf to format your number to a string, or printf to format it and display the string immediatly.

    You’d have to specify a format such as this one, I’d say : %02d :

    • padding specifier = 0
    • width specifier = 2
    • integer = d

    (Even if you have what you want here, you should read the manual page of sprintf : there are a lot of possibilities, depending on the kind of data you are using, and the kind of output formating you want)

    And, as a demo, if temp.php contains this portion of code :

    <?php
    for ($number = 1; $number <= 16; $number++) {
        printf("%02d\n", $number);
    }
    

    Calling it will give you :

    C:\dev\tests\temp>php temp.php
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP 5, I use intval() whenever I get numbers as an input. This
Watch following code: $a = 'Test'; echo ++$a; This will output: Tesu Question is,
I have this code in PHP $max=SELECT MAX(num) FROM info; $maxquery= mysql_query($max) or die
The number is bigger than int & long but can be accomodated in Decimal
I have a number of tracks recorded by a GPS, which more formally can
I'm having a problem with some code that used to work in PHP 4.X
I am trying to find a way to get the source code for (user
can some one please give me advice on how I make this block of
I have this code that creates the navigation for a slideshow. It works just
What number of classes do you think is ideal per one namespace branch? At

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.