How to always show less than 2 position int number using php ?
This below code
<?PHP
for($i=0;$i<=100;$i++)
{
echo $i."<BR>";
}
?>
result will be like this
0
1
2
3
4
5
6
7
8
9
10
.
.
.
100
I want to always show less than 2 position int number. like this how can i apply my php code ?
01
02
03
04
05
06
07
08
09
10
.
.
.
100
Just paste the lines inside your loop
if ($i < 10) {
$i= str_pad($i, 2, "0", STR_PAD_LEFT);
}
And print $i.
I don't know for sure if this works, but you can try it like this:
for($i=0;$i<=100;$i++)
{
if($i < 10) {
$i = "0$i";
echo $i;
}
else {
echo $i."<BR>";
}
}
You can use the function sprintf or the function str_pad like this ...
<?PHP
for ($i = 0; $i <= 100; $i++)
{
echo sprintf('%02d', $i) . "<BR>";
}
?>
... or this ...
<?PHP
for ($i = 0; $i <= 100; $i++)
{
echo str_pad($i, 2, '0', STR_PAD_LEFT) . "<BR>";
}
?>
Credits: https://stackoverflow.com/a/1699980/5755166
You could checkout the sprintf function that allows you to format the output http://php.net/manual/en/function.sprintf.php
Something like this perhaps
echo sprintf("%'.02d\n", 1);
You can use str_pad for adding 0's:
str_pad($var, 2, '0', STR_PAD_LEFT);
The 0 will not be added if the length is greater or equal 2.
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>";
}
usually php for loop output is 1,2,3,4,5,6,7,8,9,10,11 but i am working in XML RPC code where date is used like 01,02,03,04,05,06,07,08,09
for ($i=1; $i<=20; $i++){
echo "The number is " . $i . "<br />";
}
out put :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
but my requirement is
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
str_pad
for ($i=1; $i<=20; $i++){
echo "The number is " . str_pad($i,2,0,STR_PAD_LEFT) . "<br />";
}
You could always run some code that will check to see if it's only a single digit (meaning it's under ten)
Something like
for ($i = 1; $i <= 20; $i++){
if($i < 10) :
echo "The number is " . '0' . $i . "<br />";
else:
echo echo "The number is " . $i . "<br />";
endif;
}
I used the syntactic sugar for my if statement, but you can just as easily replace the colons with curly braces! Hope that helps!
You can use str_pad to Pad the numbers from 1-9 with 0
$value = $i;
if ($i < 10) {
$value = str_pad($i, 2, "0", STR_PAD_LEFT);
echo "The number is " . $value . "<br />";
}
I'd use printf:
for ( $i = 1; $i <= 20; $i++ ){
printf( "The number is %02u <br />\n", $i );
}
The 02 specifies the minimum number (2) of leading characters ("0"). If you end up needing more leading zeros, just change the number. I think this is most readable syntax.
What i want to print is
1
3 5
7 9 11
With my current code , that is ...
<?php
function Odd($limit='20'){
$c = 1;
while($c <= $limit){
if ($c % 2!=0){
echo $c ;
echo "<br/>";
}
$c++ ;
}
}
Print Odd();
?>
i am getting
1
3
5
7
9
11
Can someone please guide me the right way ?
Aaah ... ok.^^ Now i got it.
Its pretty easy: You need another variable which counts up and one which limits the breakposition. Looks like this:
<?php
function Odd($limit='40'){
$c = 1;
$count = 0;
$break = 1;
while($c <= $limit){
if ($c % 2!=0){
echo $c . " ";
$count++;
if($count === $break) {
echo "<br/>";
$break++;
$count = 0;
}
}
$c++ ;
}
}
Print Odd();
?>
Output till 40:
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
31 33 35 37 39
Edit: Code for your new request:
<?php
function Odd($limit='40'){
$c = 1;
$count = 0;
$break = 1;
while($c <= $limit){
echo $c . " ";
$count++;
if($count === $break) {
echo "<br/>";
$break++;
$count = 0;
}
$c++ ;
}
}
Print Odd();
?>
So if I understand correctly you want to output something like that:
1
3 5
7 9 11
13 15 17 19
Here is my solution:
function Odd($limit='20'){
$c = 1;$some_array = array();
while($c <= $limit){
if ($c % 2!=0){
$some_array[]=$c;
}
$c++ ;
}
return $some_array;
}
$array = Odd();
$nr =0;
$j=1;
foreach ($array as $key => $value) {
echo $value.' ';$nr++;
if($nr==$j){
echo '<br />';
$nr=0;
$j++;
}
}
Hope this helps!
From your question it Seems you are really new to programming so before writing any program first of all observe the question properly:
For example for the question above it is clear that is an triangle of odd numbers.
now the number of odd numbers on each row is equal to the row
i.e 1st row contains 1 number ,2nd contains 2 and it continues...
Now what we do is take an variable to count the no of rows say $row and the other will be $limit .
<?php
function odd($limit){
$row=1;
$current_number=1;
while($current_number<=$limit){
for($i=1;$i<=$row;$i++){
echo $current_number." ";
$current_number=$current_number+2;//incrementing numbers by 2 if you want to increment by 1 i.e print all numbers replace 2 by 1
}
$row++;
echo "<br/>";//for new line
}
}
To run above function you need to call it and pass the value of $limit.To do it just type anywhere outside of this function.
odd(20);
Watch this running here:
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.
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>";
}
?>