Forum Moderators: coopster

Message Too Old, No Replies

count using php

count using php

         

nick_laws

1:40 am on Sep 3, 2008 (gmt 0)

10+ Year Member



i want to count like this
0000001
0000002
0000003
0000004
0000005
0000006
0000007
.........
how i can, please understand the question i don't like this
1
2
3
4...

eelixduppy

3:00 am on Sep 3, 2008 (gmt 0)



Is this just for printing to the browser? Take a look at sprintf [php.net].

henry0

12:31 pm on Sep 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to respect the number of zero in front of each number
you could preg_replace() [php.net] by adding those zeros.

but beware you might need a switch [php.net] in order to figure if the digits are single (1,2,3 etc....)
or (10, 11, 12 etc...) or (100, 101 etc..)
and add the appropriate number of zero.

<edit>
forgot: the above will also call for a loop, since you need figuring how many numbers will be changed.
hmmm, it sounds too complicated, any other alternative?
</edit>

penders

1:23 pm on Sep 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you want to respect the number of zero in front of each number
you could preg_replace() by adding those zeros.

Or str_pad() [uk.php.net] which can do just that. Or even sprintf as eelixduppy suggests. sprintf is more concise.

eg.

$num = 2; 
echo str_pad($num,7,'0',STR_PAD_LEFT);
echo '=';
echo sprintf('%07u',$num);

Note that 0000002 is essentially a string, not a number. You count like 1 2 3 4, but display it as '0000001' '0000002' '0000003' '0000004'.