Hi
I'm guessing this is relatively easy to do, but i can't find the right command.
Basically i have a number:
$i = 1
i want to keep adding to that number in a loop but i want it to give me:
01 02 03 04 ...
rather than
1 2 3 4
how can i do this? If i make $i=01 i then get:
01 2 3 4
Thanks
i believe [man]for[/man] is what your looking for.
for ($i=0;$i<=4;$i++) { echo '0'.$i.'<br>'; }
Exactly but i also want to keep $i to a max length of two, else with your solution you'd get
.. 08 09 010 011 ..
instead of
.. 08 09 10 11 ...
sorry for not mentioning this earlier.
Ant
for ($i=0;$i<=20;$i++) { if($i < 10) echo '0'.$i.'<br>'; else echo $i.'<br>'; ]
if ($i < 10 ){ echo '0'.$i; }else { echo $i; }
Try using this instead of looping through all the values...
str_pad($i, 2, 0, STR_PAD_LEFT)
so...
<?php for ($i=1; $i<105; $i++) echo str_pad($i, 2, 0, STR_PAD_LEFT),"<br>"; ?>
would give you 01 02 ... 10 11 ... 100 101 etc.
Alternatively you could use [man]printf/man, e.g.
printf("%'02d", $i);
dhodg say: Try using this instead of looping through all the values... <?php for ($i=1; $i<105; $i++) echo str_pad($i, 2, 0, STR_PAD_LEFT),"<br>"; ?>
dhodg say:
Isn`t this a loop?
Depends on what you are doing with these values where they are coming from etc... Might or might not have to use a loop. Either way str_pad is the cleaner way to do it.
You guys are ace, thanks!
used dhodge's method, cheers man!! Thats exactly what i was looking for!