for ($i=1; $i<=4; ++$i) {
echo "The number is " . $i . "\n";
}
This will output:
The number is 1
The number is 2
The number is 3
The number is 4
How can i make a loop that will give me output like so:
The number is 1
The number is 1
The number is 1
The number is 1
The number is 2
The number is 2
The number is 2
The number is 2
etc
Thanks for any help.
Without nested loops: this would suffice for a single loop.
for($i=0;$i<9*4;$i++)
{
echo "The number is ".(1+floor($i/4));
}
So you want
for ($i=1; $i<=2; ++$i) {
echo "The number is " . $i . "\n";
echo "The number is " . $i . "\n";
echo "The number is " . $i . "\n";
echo "The number is " . $i . "\n";
}
But let's avoid the repetition with a loop!
for ($i=1; $i<=2; ++$i) {
for ($j=1; $j<=4; ++$j) {
echo "The number is " . $i . "\n";
}
}
Essentially, you want to print something four times inside the loop... so you can write four echo statements. A better way to do this would be to use nested for loops.
for ($i=1; $i<=4; ++$i) {
for ($j=1; $j<=4; ++$j) {
echo "The number is " . $i . "\n";
}
}
For every iteration of the outer loop, the inner one prints the statement four times. One thing to be careful with nested loops is the variables used in the conditions. If you mix them up, you could have weird issues including an infinite loop.
You need two nested loops, like so:
for( $i = 1; $i <= 4; ++$i) {
for( $j = 1; $j <= 4; ++$j) {
echo "The number is " . $i . "\n";
}
}
One of a million of possible solutions could be using single loop and str_repeat() function.
for ($i=1; $i<=4; $i++)
echo str_repeat("The number is $i\n", 4);
which is probably the best way to make multiple repeats of same string.
you can make two loops
for($i = 1; $i <= 4; $i++) {
for($j = 1; $j <= 4; $j++) {
echo 'The number is '.$i."\n";
}
}
You've got the same loop, but four iterations within:
for ($i=1; $i<=4; ++$i) {
for($j=0;$j<4;$j++) {
echo "The number is " . $i . "\n";
}
}
Related
How I can make pyramid number like this
1
212
32123
Code:
<?php
for($i=2; $i<=5; $i++){
for($x=$i; $x>=2; $x--){
echo "$x ";
}
echo "<br/>";
}
for($i=1; $i<=5; $i++){
for($x=1; $x<=$i; $x++){
echo "$x  ";
}
echo "<br/>";
}
You seem to printing numbers in descending order, adding a new line and then go to print numbers in ascending order in another line. This makes it too late to do so since you already lost the current line.
Instead, for every row, go from row number till 1 and print numbers and repeat the same in the same line from 2 till row number and then add a new line like below:
<?php
function printPyramid($rows){
for($i = 1; $i <= $rows; ++$i){
echo str_repeat(" ", $rows - $i);
for($j = $i; $j >= 1; --$j){
echo $j;
}
for($j = 2; $j <= $i; ++$j){
echo $j;
}
echo PHP_EOL; // or echo "<br/>";
}
}
printPyramid(7);
Online Demo (view the output in HTML format by clicking on the eye icon)
What I want is the first loop iterating from 1 to 4 and the second loop from 5 to 6.
Here is my code:
<?php
for ($i = 1 ; $i <= 4 ; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
for ($i = 1 ; $i <= 2 ; $i++)
{
echo $i . "<br>";
}
?>
The loops you've given are:
1st loop: from 1 to 4
2nd loop: from 1 to 2
First loop is ok, but seconds needs to be modified. Use $i<=6 and don't initialize $i variable.
This will give you:
1st loop: from 1 to 4
2nd loop: from (value that 1st loop have ended)+1 to 6, so (4+1) to 6, 5 to 6
<?php
$i = 0; // be sure 'i' is visible in both loops
for ($i=1; $i<=4; $i++) // form 1 to 4
{
echo $i . "<br>";
}
?>
<hr>
<?php
$i++; // start from 5, not 4
for (; $i<=6; $i++) // from the previous value to 6
{
echo $i . "<br>";
}
?>
Your problem
The second for loop resets your $i variable to 1:
for ($i = 1 ; $i <= 2 ; $i++)
Solution
You can use a while loop instead of your second for loop:
<?php
for ($i = 1; $i <= 4; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
while ($i <= 6) // `<= 6` instead of `<= 2`, since we keep $1's value
{
echo $i . "<br>";
$i++;
}
?>
Rather than using two loops for this, why not just output the <hr> tag at the appropriate point within the same one? If you carry on with adding extra loops, first of all you'll run into confusing problems like this about (re-)initialising variables, and you'll also quickly end up with a lot of unnecessary duplicated code.
You can use the PHP modulo operator (%) to output the <hr> tag after every fourth element, which will both reduce the complexity and be a lot more extensible if you later add more elements:
for ($i=1; $i<=6; $i++) {
echo $i . "<br>";
if ($i % 4 === 0) {
echo "<hr>";
}
}
See https://eval.in/976102
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;
}
?>
I would like to know, how to start looping of a php array with the second item.
Instead of the standard for loop to go through an array, for example:
for($i = 0; $i < count($array); $i++) {
echo $array[$i] . " ";
}
Outputs "one two three four five".
Just set $i equal to 1 instead, like so:
for($i = 1; $i < count($array); $i++) {
echo $array[$i] . " ";
}
Now it outputs this: "two three four five".
However, if you wanted to get the last four items of an array, you can use array_slice().
$sliced = array_slice($array, -4);
for($i = 0; $i < count($sliced); $i++) {
echo $sliced[$i] . " ";
}
Outputs "two three four five".
I am trying to make a triangular-shaped set of lines of decreasing numbers like this :
5
45
345
2345
12345
I tried this :
for($i=1;$i<=5;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j;
}
echo "<br>";
}
But it is printing the low number first and appending increasing numbers like this :
1
12
123
1234
12345
The inner loop needs to count down instead of up.
You can either subtract the outer loop's variable from the limit to get the starting point and count down:
for ($i = 0; $i < 5; $i++)
{
for ($j = 5 - $i; $j > 0; $j--)
{
echo $j;
}
echo "<br>";
}
or change the outer loop to count down from the limit as well.
for ($i = 5; $i >= 1; $i--)
{
for ($j = $i; $j >= 1; $j--)
{
echo $j;
}
echo "<br>";
}
This is pretty straightforward:
$max = 5;
echo "<pre>";
for($line=0; $line<$max; $line++) {
$min_this_line = $max-$line;
for($num = $min_this_line; $num <= $max; $num++) {
echo $num;
}
echo "\n";
}
echo "</pre>";
Output:
5
45
345
2345
12345
I think I would declare the $peak value, then use a for() loop to decrement the counter down to 1, and use implode() and range() to build the respective strings inside the loop.
This isn't going to outperform two for() loops, but for relatively small $peak values, no one is going to notice any performance hit.
Code: (Demo)
$peak = 5;
for ($i = $peak; $i; --$i) {
echo implode(range($i, $peak)) , "\n";
}
or with two loops: (Demo)
Decrement the outer loop and increment the inner loop.
$peak = 5;
for ($i = $peak; $i; --$i) {
for ($n = $i; $n <= $peak; ++$n) {
echo $n;
}
echo "\n";
}
Both output:
5
45
345
2345
12345