For Loop starting from 0001 instead of 1 [duplicate] - php

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...

Related

Loop Removes Zero Before Integer [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 6 years ago.
If my loop runs 3 times, then the output is 0001,2,3, but I need
0001
0002
0003
How do I get this output?
$i='0001';
foreach($test as $rows){
echo $i;
echo '<br/>';
$i++;
}
Use printf with a padding specifier, e.g.:
$test = range(1,11);
$i = 0;
foreach ($test as $rows) {
printf("%04d<br/>\n", ++$i);
}
Output
0001<br/>
0002<br/>
0003<br/>
0004<br/>
0005<br/>
0006<br/>
0007<br/>
0008<br/>
0009<br/>
0010<br/>
0011<br/>
In this example, 04 is a padding specifier meaning that the number (d) is padded with maximum 4 zeroes.
You can use str_pad()
$i = "0001";
for($j=0;$j<1000;$j++) {
echo str_pad($i++, 4, '0', STR_PAD_LEFT);
echo "<br/>";
}
You can cheat a little bit,
$i = "1";
$y = "000"; //Added this
while ($i < 4){
echo $y . $i;
$i++;
}
Basically you echo the 000 in front of the number each time.

Incrementing numbers with double digits in php [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 7 years ago.
I would like to Increment numbers with double digits if the number is less then 10
This is what i tried so far
$i = 1;
echo $i++;
results is 1,2,3,4,5,6 so on
Then i try adding a condition
$i = 1;
if ($i++<10){
echo "0".$i++;
}else{
echo $i++;
}
Work but skipping the numbers 2,4,6,8 so on.
Can anyone tell me the proper way to do this?
If the condition is only there for the leading zero you can do this much easier with this:
<?php
$i = 10;
printf("%02d", $i++);
?>
if you want prepend something to a string use:
echo str_pad($input, 2, "0", STR_PAD_LEFT); //see detailed information http://php.net/manual/en/function.str-pad.php
On the second fragment of code you are incrementing $i twice, that's why you get only even numbers.
Incrementing a number is one thing, rendering it using a specific format is another thing. Don't mix them.
Keep it simple:
// Increment $i
$i ++;
// Format it for display
if ($i < 10) {
$text = '0'.$i; // Prepend values smaller than 10 with a zero
} else {
$text = $i;
}
// Display it
echo($text);
<?php
$i = 1;
for($i=1;$i<15;){
if($i<10){
echo '0'.$i++."<br>";
}else{
echo $i++."<br>";
}
}
?>

For Loop : increment a long number retaining 0's in 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);
}

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);

PHP set int length

Alright simple question
I have integriters like
5
188
4634
And they all need to be formated to
0000000005
0000000188
0000004634
It can become a string that doesnt matter.
sprintf is the function for that:
$num = sprintf("%010d", $num);
str_pad
echo str_pad($str, 10, "0",STR_PAD_LEFT);
<?php
#how many chars will be in the string
$filltotal = 10;
$number = 5;
#with str_pad function the zeros will be added
echo str_pad($number, $fill, '0', STR_PAD_LEFT);
// The result: 0000000005
Alternative is str_pad:
echo str_pad($num, 10, "0", STR_PAD_LEFT);
you can achieve by this.
$val = 12;
for($i=0;$i<(10 - count($val));$i++)
{
$str .= '0';
}
$final_val = $str.$val;

Categories