tags:

views:

99

answers:

8

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
+3  A: 

You can use str_pad().

str_pad — Pad a string to a certain length with another string

This functions returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit.

Pekka
A: 

if($number < 10) echo '0' . $number;

I know there are a lot better answers than this here, but I though't I'd leave it to show there's more than one way to skin a cat...

Skilldrick
This needs an else branch. Otherwise numbers > 10 aren't printed at all.
VolkerK
I know. I assumed that Andrew would be able to work out the rest... It's just meant to be a snippet.
Skilldrick
A: 
<?php
  for ($number = 1; $number <= 16; $number++) 
  { 
    if($number<10)
      echo '0';
    echo $number . "<br>"; 
  }
?>
Dave
That would give you 010, 011, 012, etc.
Sean Bright
:-) you are correct! Updated it.
Dave
please use ctrl+k to mark code blocks (or indent the code manually with 4 spaces)
VolkerK
@Dave: you see how the printf approach is superior, I hope?
Sean Bright
absolutely, much cleaner code! I didn't know about str_pad(), which seems really handy.
Dave
+2  A: 

The old-fashioned way: $str = sprintf("%02.2d", $number); or use printf("%02.2d\n", $number); to print it immediately.

Chris
+9  A: 

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
Pascal MARTIN
+1 for explaining the format.
Skilldrick
A: 

Quick method:

<?php
for ($number = 1; $number <= 16; $number++)
{    
    if($number < 10)
        echo "0".$number."\n";
    else
        echo $number."\n";
}
?>
Urda
Snap :) ........
Skilldrick
I'm being down-voted because?
Urda
@Urda: Because it is inefficient.
Johannes Gorset
I'm going to go out on a limb here, I don't think efficiency is OP's highest worry. But hey it was just a simple answer for a simple question. Sorry your nose is too high up in the clouds at this point though!
Urda
A: 
for ($number = 1; $number <= 16; $number++) {
  echo sprintf("%02d", $number) . "<br>";
}
Keith Maurino
A: 

Lets keep it simple. Use str_pad

echo str_pad(1, 6 , "0");

produces 000006

Ronaldo Junior