Simple PHP counter - how to print numbers with loop? - php

How can I print out the numbers from 0 to 20 with a loop in PHP?
I hope some one can help.
Thanks!

You want a for loop:
http://php.net/manual/en/control-structures.for.php

Use a for loop:
for ($i = 0; $i <= 20; $i++)
{
echo $i . "\n";
}

Related

PHP even numbers generating

I'm somewhat new to PHP, been reading a few books and I've never seen a loop where it gets you all the even numbers(for example from 1 to 10), so I decided to try it myself:
for($i=0;$i<10 && $i % 2===0;$i++)
echo $i;
Tried with only double == as well.
And this,
$i=0;
do echo $i; while($i++<10 && $i % 2 ==0);
Can't seem to figure out how to use 2 conditions in the same statement.
Would appreciate the help!
Thanks.
Try to use this code
for( $i=0; $i<=10; $i++ )
{
if( $i%2 == 0 ){
echo $i;
}
}
The loop is breaking entirely when the second condition fails the first time. On the first iteration: 0 is less than 10, and it is even, so the loop iterates. On the second iteration: 1 is less than 10, but is odd, so the loop breaks.
Your code is the equivalent of this:
for($i=0; $i<10; $i++) {
if ($i % 2 !==0 ) {
break;
}
echo $i;
}
0
You can eliminate the second condition of your for loop to prevent the breakage and rely exclusive on a third expression to increment $i by two each iteration.
for($i=0; $i<10; $i = $i + 2) {
echo $i;
}
02468
The second statement in a for-loop is/are the condition(s) which gets checked every loop. so if it fails your loop stops. what you need will look somewhat like this:
for ($i = 0; $i < 10; $i++)
if ($i % 2 == 0)
echo $i;
So the loop will run over every number but only print out the even ones.
You don't need to loop.
Range can create a range with third parameter step 2.
$arr = range(0,20,2);
Echo implode(" ", $arr);
https://3v4l.org/S3JWV
you can use also regular loop and get the evens by formula:
for($i=0; $i<10 ;$i++) {
$j = $i * 2;
// do somthing with $j witch loop over 10 first evens...
}

how to fix php for loop output displaying 1?

Here is my php code given below when I run it in my browser its display 1 continuously. My question is why its show like this? After execution its displays a fatal error : maximum execution time is exceeded. what is that?
<?php
for ($i=1; $i<=5; $i+1) {
echo $i." ";
}
?>
Give me proper answer. Make me sure that what is the execution time of php code?
TIA
$i+1 does not increment the value of $i. It only adds 1 to what is in $i but it does not assign it back to $i. Your loop does this:
$i = 1
while ($i<=5) {
echo $i." ";
$i+1;
}
$i+1 on it's own doesn't do anything.
You need something like $i = $i + 1. Or for short $i += 1. Or even shorter and better: $i++.
for ($i=1; $i<=5; $i++) {
echo $i . " ";
}
It is because of $i+1 in for loop. This is basically an expression and it produces a result but you never assign this result to $i. Therefore you would rather do something like $i = $i + 1 or, in real life, use incrementation $i++. So the final code will looks like:
for ($i = 1; $i <= 5; $i++) {
echo $i." ";
}
use $i++ not $i+1, $i++ is $i=$i+1
Change your code to
<?php
for ($i=1; $i<=5; $i++) {
echo $i." ";
}
?>
because first you must set value for $i
You need to do
<?php
for( $i = 1; $i <= 5; $i++) {
echo $i." ";
}
?>
Now you just say 1 + 1 but you don't assign it to anything. You could use $i = $i + 1 but it's the same as $i++
<?php
for ($i=1; $i<=5; $i++) {
echo $i." ";
}
?>
in a for loop, every turn you need to increase value of $i. but you forgot to increase value of $i. You wrote $i +1 but it needs to be assigned with new value of $i.
In short, you should change $i +1 to $i = $i +1 or $i ++
the right code:
<?php
for ($i=1; $i<=5; $i = $i+1) {
echo $i." ";
}
?>
You are not changing the value of $i in the loop. Either $i =$i +1 or $i++ instead of $i + 1 will do.
You've problem in printing the line. It should look like
<?php
for ($i=1; $i<=5; $i++) {
echo $i;
}
?>

How to random numbers in php between two block and make the results unique?

I need to create 2 blocks and each block contain number between 1-3. how can i achieve this with php?
the results should be:
1-1
1-2
1-3
2-1
2-2
2-3
3-1
3-2
3-3
many thanks for your help
Nest for loops and echo the loop values.
For($i=1;$i<=3;$i++){ //outer loop
For($j=1;$j<=3;$j++){ // inner loop
Echo $i ."-".$j ."\n"; // echo inner value and outer value
}
}
https://3v4l.org/2Kqbl
I am not sure what do you mean by random, but to get the desired out put you can use nested for loop.
$m=3;
$n=3;
for($i=1;$i<=$m;$i++){
for($j=1;$j<=$n;$j++){
echo $i .'-'. $j."\n";
}
}
Use nested for loop Example:
for($i = 1; $i <= 3 ; $i++){
for($j = 1; $j <= 3; $j++){
echo $i .'-'. $j."\n";
}
}
depends on what do you want to be the relation between that 2 "blocks" or what they are propose for... what you have posted there can be made like this:
$rand = rand(0,3);
for($x = 0; $x <= 9; $x++;){
echo $rand."-".$rand; echo"<br>";
}

PHP loop X amount of times

I have a string called $columns which dynamically gets a value from 1 to 7. I want to create a loop of <td></td> for however many times the value of $columns is. Any idea how I can do this?
for ($k = 0 ; $k < $columns; $k++){ echo '<td></td>'; }
Here's a more readable way to achieve this:
foreach(range(1,$columns) as $index) {
//do your magic here
}
If you just need to use number of repeat count:
for ($i = 0; $i < 5; $i++){
// code to repeat here
}
just repeat $n times? ... if dont mind that $n goes backwards...
the advantage is that you can see/config "times" at the beginning
$n = 5;
while (--$n >= 0)
{
// do something, remember that $n goes backwards;
}
I like this way:
while( $i++ < $columns ) echo $i;
Just bear in mind if $columns is 5, this will run 5 times (not 4).
Edit: There seems to be some confusion around the initial state of $i here. You are welcome to initialise $i=0 beforehand if you wish. This is not required however as PHP is a very helpful engine and will do it for you automatically (tho, it will throw a notice if you happen to have those enabled).
There is a str_repeat() function in PHP, which repeats a string a number of times. The solution for your problem would be:
str_repeat( '<td></td>', $columns );
If $columns is a string you can cast to int and use a simple for loop
for ($i=1; $i<(int)$columns; $i++) {
echo '<td></td>';
}
A for loop will work:
for ($i = 0; $i < $columns; $i++) {
...
}
You can run it through a for loop easily to achieve this
$myData = array('val1', 'val2', ...);
for( $i = 0; $i < intval($columns); $i++)
{
echo "<td>" . $myData[$i] . "</td>";
}
Why use logic at all, don't waste those CPU cycles!
<td colspan="<?php echo $columns; ?>"></td>

0's Not being Stripped from the front of a numeric value

I am working on a project for a friend's website which is suppose to generate completely random phone numbers to be displayed on a "fake" review board. I figured the best way to do with would be for me to to generate out each section separably. So 3-3-4, but no matter what I do, every time there is a 0 in front the code cuts it off. Here's an example of what I mean:
http://www.shiningashes.net/Test.php
yet this is what I have for the code:
<?php
for ($i = 0000; $i <= 9999; $i++) {
echo $i;
echo "<br>";
}
?>
How do I get the 0's to stop being cropped out so the 0's display? 0001, 0021, 0123, etc?
You can use str_pad
for ($i = 0; $i <= 9999; $i++) {
echo str_pad($i, 4, '0', STR_PAD_LEFT);
echo "<br>";
}
You can use printf to format your output:
<?php
for ($i = 0; $i <= 9999; $i++) {
printf("%04d<br>\n",$i);
}
?>
You need to make your variable a string if you want to keep the zeros. That would mean using quotes, and never using numeric operators on it. But since you depend on using ++ on it, I suggest the following hack:
<?php
for ($i = 10000; $i <= 19999; $i++) {
$str=substr ( $i , 0 , 4 );
echo $i;
echo $str;
echo "<br>";
}
?>
You will need to convert your integer to a string when printing it.
<?php
for ($i = 0; $i <= 9999; $i++) {
printf("%04d<br />", $i);
}
?>
Check the documentation for printf/sprintf for more information.
Kind regards,
Stefan

Categories