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.
Related
I need a output like this
Number 5
Number 4
Number 9
Number 3
Number 8
Number 10
And so on (There are more like this)
I used this code
<?php
for ($i = 0; $i <= 10 ; $i++) {
if ($i == 5 || $i == 4 || $i == 9) { //And so on Like this
echo "$i<br>";
}
}
?>
But the main problem is output is showing the number serially.
//It shows
Number 3
Number 4
Number 5
Number 8
Number 9
Number 10
//But I need
Number 5
Number 4
Number 9
Number 3
Number 8
Number 10
And this takes much time to code. And it not looks so good. Sure there is a easy way out!
I am expecting something like this -
//Surely this is not right. It's just an idea.
<?php
$x = 5,4,9,3,8,10;
for ($i = 0; $i = $x; $i++) {
echo "$i<br>";
}
?>
Take this
$x = array(5,4,9,3,8,10);
foreach ($x as $i) {
echo "Number $i<br>";
}
but please, learn the basics of PHP if you really want to code in php.
JustOnUnderMillions's answer is corrent - You can even have access to key & value like this
$x = array(
"num1" => 1,
"num2" => 2,
...
...
);
foreach($x as $key => $value){
echo $key . " : " . $value . "<br>";
}
This question already has answers here:
Generating UNIQUE Random Numbers within a range
(14 answers)
Closed 7 years ago.
I am trying to find a solution in PHP that can generate three random numbers.
At the moment I have this that generates a random number that is different from $randNum;
The numbers need to be different from each other and also different from the variable $randNum
Thank you
$wrong = $randNum;
while ($wrong == $randNum) {
$wrong = rand(0,$max - 1);
}
<?php
$numbers = [];
for($i = 0; $i < 10; $i++){
$number = rand (1,15);
while (in_array($number, $numbers)){
$number = rand (1,15);
}
$numbers[] = $number;
}
echo '<pre>';
print_r($numbers);
echo '</pre>';
This function generates unique 10 random numbers, range from 1-15 you can easy change this script to your needs.
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>";
}
}
?>
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 8 years ago.
I'm trying to get the results of the for loop like: 00001, 00002, 00003, etc. but the result is not displaying 0's: instead I get: 1, 2, 3, etc.
This is the code:
$min = 00001;
$max = 00005;
for ($x = $min; $x <= $max; $x++) {
echo "$x ";
}
Just change your echo statement to this:
echo sprintf('%05d', $x);
Also i would recommend you to change your $min and $max variables to this:
$min = 1;
$max = 5;
Because if you have leading zeros then number gets interpreted as an octal number! So 00012 would not be 12 it would be 10.
<?php
$min = 00001;
$max = 00005;
for ($x = $min; $x <= $max; $x++) {
printf("%5d",$x);
}
?>
this is new code
<?php
$min = 00001;
$max = 00005;
for ($x = $min; $x <= $max; $x++) {
printf("%05d",$x);
echo '<br>';
}
?>
Thanks a lot for the help. Doing the trick via MySQL got everything done. Just had to change the value to decimal and assign the field size (7) with the corresponding zerofill attribute.
Now i can insert the count of the for loop without zeros but the db inserts them for me. Thanks
This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
for ($number = 1; $number <= 16; $number++) {
echo $number . "\n";
}
This code outputs:
1
2
3
...
16
How can I get PHP to output the numbers preceded by zeros?
01
02
03
...
16
You could use sprintf to format your number to a string, or printf to format it and display the string immediatly.
You'd have to specify a format such as this one, I'd say : %02d :
padding specifier = 0
width specifier = 2
integer = d
(Even if you have what you want here, you should read the manual page of sprintf : there are a lot of possibilities, depending on the kind of data you are using, and the kind of output formating you want)
And, as a demo, if temp.php contains this portion of code :
<?php
for ($number = 1; $number <= 16; $number++) {
printf("%02d\n", $number);
}
Calling it will give you :
C:\dev\tests\temp>php temp.php
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
You can use str_pad().
str_pad — Pad a string to a certain length with another string
This functions returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit.
The old-fashioned way:
$str = sprintf("%02.2d", $number);
or use
printf("%02.2d\n", $number);
to print it immediately.
for ($number = 1; $number <= 16; $number++) {
echo sprintf("%02d", $number) . "<br>";
}
if($number < 10) echo '0' . $number;
I know there are a lot better answers than this here, but I though't I'd leave it to show there's more than one way to skin a cat...
Quick method:
<?php
for ($number = 1; $number <= 16; $number++)
{
if($number < 10)
echo "0".$number."\n";
else
echo $number."\n";
}
?>
Lets keep it simple.
Use str_pad
echo str_pad(1, 6 , "0");
produces 000006
echo ($number < 10) ? (0 . $number) : $number;
<?php
for ($number = 1; $number <= 16; $number++)
{
if($number<10)
echo '0';
echo $number . "<br>";
}
?>