-1

I'm trying to get the results of the for loop like: 00001, 00002, 00003, etc. but the result is not displaying 0's: instead I get: 1, 2, 3, etc.

This is the code:

$min = 00001;
$max = 00005;

for ($x = $min; $x <= $max; $x++) {
    echo "$x ";
}
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
narclax
  • 43
  • 6
  • you need to use `str_pad` – Vijayaragavendran Dec 22 '14 at 13:26
  • 1
    Leading zeroes aren't just eliminated; any integer starting with a zero is interpreted as an Octal number. That means that 0010 isn't representing the decimal number 10, but the decimal number 8! Never prepend PHP ints with 0 unless you're working with Octals. – Erik Dec 22 '14 at 13:29
  • Actually i will be working with higher values like $max = 0387648 – narclax Dec 22 '14 at 13:36
  • *"Actually i will be working with higher values like `$max = 0387648`"* - why the leading zero? Again, as Erik states and Rizier's answer below; leading zeros are treated as octals. Have you tried Rizier's answer? @narclax – Funk Forty Niner Dec 22 '14 at 13:53
  • I think i will need to work with octals, i need to work with zeros because the application needs to insert the exact serial for some labels to be printed. – narclax Dec 22 '14 at 13:57
  • 1
    You know you can achieve that in MySQL, having leading zeros as the column's leading character. There are a few ways of doing this, and in PHP also. I've seen it before; it's a tad complex, but that may be what you need to do instead. Using octals might not produce the desired results. Are you 100% sure you want to use octals? @narclax – Funk Forty Niner Dec 22 '14 at 14:06
  • No, actually i'm hoping to get some better way to approach this issue. What method can you advise me? – narclax Dec 22 '14 at 14:07
  • See the Q&A's here http://stackoverflow.com/q/11165104/ and http://stackoverflow.com/q/6873354/ and http://stackoverflow.com/q/10944167/ @narclax – Funk Forty Niner Dec 22 '14 at 14:10
  • Thanks a lot for the help. Doing the trick via MySQL got everything done. Just had to change the value to decimal and assign the field size (7) with the corresponding zerofill attribute. now i can insert the count of the foorloop without zeros but the db inserts them for me. Thanks Fred!! – narclax Dec 22 '14 at 14:48

3 Answers3

6

Just change your echo statement to this:

echo sprintf('%05d', $x);

Also i would recommend you to change your $min and $max variables to this:

$min = 1;
$max = 5;

Because if you have leading zeros then number gets interpreted as an octal number! So 00012 would not be 12 it would be 10.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
1
<?php
$min = 00001;
$max = 00005;
for ($x = $min; $x <= $max; $x++) {
printf("%5d",$x);
}
?>

enter image description here

this is new code

<?php
$min = 00001;
$max = 00005;
for ($x = $min; $x <= $max; $x++) {
printf("%05d",$x);
echo '<br>';
}
?>

enter image description here

Sharma Vikram
  • 2,440
  • 6
  • 23
  • 46
1

Thanks a lot for the help. Doing the trick via MySQL got everything done. Just had to change the value to decimal and assign the field size (7) with the corresponding zerofill attribute.

Now i can insert the count of the for loop without zeros but the db inserts them for me. Thanks

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
narclax
  • 43
  • 6