For Loop : increment a long number retaining 0's in php - php

How to increment through LONG number in PHP, for example counter starts from 0000000001 and ends 0000000010, how to loop through in a for loop retaining the zero's.
I do not want to pad 0's in front of the number, because I can increase the number limit to 9999999999999.
for ($i=0000000001; $i<=0000000010; $i++) {
echo $i;
}
Output:
0000000001
0000000002
0000000003
0000000004
0000000005
0000000006
0000000007
0000000008
0000000009
0000000010
Can someone please help me.

You can use str_pad:
str_pad($value, 8, '0', STR_PAD_LEFT);
OR if it's just for output:
sprintf('%08d', 1234567);
OR
echo str_pad($value, 8, '0', STR_PAD_LEFT);
EDIT
for ($i=1; $i<=10; $i++) {
echo str_pad($i, 10, '0', STR_PAD_LEFT);
}

Related

How to make sequential number and repeat it

I would like to make a number sequential and repetitive to the beginning when it reaches the limit. For instance, the first number begins with 001, then 002, 003, and so on until it reaches 999. When it reaches 999, then the number will return to 01 and continues to repeat.
I currently have this loop:
$i = 001;
for ( $i; $i < 111; $i++)
{
echo str_pad ($i, 3, '0', STR_PAD_LEFT), '<br>';
}
But how can I make that return to the first number and continues like before? I mean if the number reaches 999, it will back to 001, 002, 003, and so on.
Even if I don't quite understand why you want to do that, this is your solution :
<?php
for ($i = 001, $max = 111; $i < $max; $i++)
{
echo str_pad ($i, 3, '0', STR_PAD_LEFT), '<br>';
if ($i == $max - 1) {
$i = 001;
}
}
But this will cause an infinite loop of course.

For Loop starting from 0001 instead of 1 [duplicate]

This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
I'm doing a for loop to count from 0001 up to 0999
How can i do this with php. All i've got so far is a normal php for loop.
Something like this?
for($i = 1; $i<=999; $i++){
echo str_pad($i, 4, '0', STR_PAD_LEFT);
}
Additionally, you can make use of sprintf() instead of str_pad(), but I think str_pad() looks much clearer than sprintf() in this case.
What you want to do is a normal loop and format the output:
for( $i=1; $i<=999; $i++) {
$myformat = str_pad($i, 4, '0', STR_PAD_LEFT);
// do something with $myformat
}
Try that code:
for($n=1;$n<=999;$n++)
{
$formatted_n = str_pad($n, 4, '0', STR_PAD_LEFT);
// add some code here
}
Documentation for str_pad
<?php
for($i=1; $i<=999; $i++) {
echo str_pad($i, 4, "0", STR_PAD_LEFT);
}
?>
Here is a version using sprintf():
foreach (range(1, 999) as $i){
echo sprintf("%04d", $i);
} // output: 000100020003...

Numbering with prefix of zero in PHP [duplicate]

This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
Is it possible in PHP to echo out a number with zeroes at the beginning?
For example
<?php
$i = 0001;
$i++;
echo $i;
?>
And when it print out, I want it to be like.
0002
Is it possible? Thanks :)
Yes, it is possible. You can do this using str_pad() method. Basically, this method adds a given value till a required length is achieved.
A Basic example of this would be:
echo str_pad($i, 4, "0", STR_PAD_LEFT);
Here,
4 represents the output length
"0" represents the string used to pad, till the length is achieved.
Demo Example:
<?php
$i = 0001;
$i++;
echo str_pad($i, 4, "0", STR_PAD_LEFT);
$i = 1;
$i++;
printf("%04d", $i); // 0002
printf - output a formatted string
%04d - echo a 4 digit number, pad with 0's
try this
echo str_pad($i, 4,"0",STR_PAD_LEFT);
read the documentation here : http://php.net/manual/en/function.str-pad.php
$input = 0001;
echo str_pad(++$input, 4, "0", STR_PAD_LEFT);

how to iterate through every x numbers and echo results in php?

the question title might be a bit confuse. but here is my case.
i would like my result to like something like:
0000000000
0000000001
0000000002
0000000003
0000000004
0000000005
0000000006
0000000007
0000000008
0000000009
0000000010
0000000001
0000000011
0000000012
0000000013
0000000014
0000000015
0000000016
0000000017
0000000018
0000000019
0000000002
0000000020
0000000021
0000000022
0000000023
...
0000000003
...
0000000004
...
i can get the first numbers by doing this:
for ($i = 1; $i<=10; $i++){
$nr=str_pad($i, 10, "0", STR_PAD_LEFT);
echo $nr.'<br>';
}
but then how do i get the numbers in between?
any ideas?
Thanks
edit: the second row of numbers are based on 10 by 10 count.
so. first record will have 10 other records underneath, the second record another 10 and so on.
i've tried something using $floor= floor($i/10); and adding that to the second set of records in here: $nr=str_pad($floor, 10, "0", STR_PAD_LEFT);
I don't quite understand what you are trying to do. But one possible solution would be this:
for ($i = 1; $i < 100; $i++){
if ($i % 10 == 1)
echo str_pad(($i-1)/10, 10, "0", STR_PAD_LEFT) . "<br />\n";
echo ' '.str_pad($i, 10, "0", STR_PAD_LEFT) . "<br />\n";
}
See it in action.
Depending on your problem, using two cycles might be better for you:
for ($i = 0; $i < 10; $i++){
echo str_pad($i, 10, "0", STR_PAD_LEFT) . "<br />\n";
for ($j = 1; $j <= 10; $j++) {
echo ' '.str_pad($i*10+$j, 10, "0", STR_PAD_LEFT) . "<br />\n";
}
}
Live.
This looks like homework, but I would change your loop as described to iterate the secondary numbers, and then use a test such as MOD ( http://php.net/manual/en/internals2.opcodes.mod.php ) to check for those times when you want to emit the first level grouping.
So you adjust your str_pad line to indent as well as left pad with 0, and add a lne near the top of the loop to check if ($i % 10 == 0) (that is, if it is divisible by 10 without a remainder).
Note this may not be the fastest nor most elegant way.

how do i use 4 digit numbers

I need to count from:
0-9999
In PHP, how can i make the format:
0000-9999
So that the output is:
0000,0001,0002,....
thanks!
$num = 9;
$paddedNum = sprintf("%04d", $num);
echo $paddedNum; // prints 0009
$number = 9;
echo str_pad($number, 4, '0', STR_PAD_LEFT); // output: 0009

Categories