Numbering with prefix of zero in PHP [duplicate] - php

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

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.

Ask about how to sum 1 + 01 =02 in PHP [duplicate]

This question already has answers here:
ask about php summarize 01 + 01 = 02
(2 answers)
Closed 7 years ago.
How get the number with format is leading zero ex:01
and how to make format number like that my problem is from this query
$try= $this->login->get_id_child('00',3);
$count_try = count($try);
$id ="00";
$new = $id.$count_try;
echo $new;
the result from echo $new is 003
data in array $try=array(000,001,002) and try is count by $count_try the result is 3. but I want to make the result from $count_try is 03 so I can join that variable to my id so I can get $new=00003 but if the data in
$try=array(000,001,002,003,004,005,006,007,008,009);
so the value of $new is 0010
You can use str_pad -
echo str_pad($new, 2, "0", STR_PAD_LEFT);
str_pad()
Update
$count_try = 10;
$new = str_pad($count_try, 4, "0", STR_PAD_LEFT);
echo $new;
Try this :
$id ="0010";
$new = (int) $id + 1;
$new = str_pad($new, 4, "0", STR_PAD_LEFT);
echo $new; //0011

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

Covert digit to x.xx format in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
simple regex in php, formatting decimal number
How to convert a digit in x format to x.xx format in php?
example :
I have a number 5. Need to convert to 5.00
$n = 5;
$n = number_format($n, 2, '.', '');
You should try number_format():
$your_number = 5;
echo number_format($your_number, 2); // displays 5.00
Manual
See http://php.net/manual/en/function.number-format.php
number_format('your digit', 2 or 1, '.', '');
$number = 125
$no = number_format($number, 2, '.', '');
output = 125.00
$x = 5;
$x = sprintf('%.2f', $x);
echo $x;

Categories