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

2
  • 1
    Not sure why there are down votes. This seems to be a legitimate beginner question. Some constructive criticism would be helpful on downvotes. +1 to offset the negativity. Commented Jun 29, 2012 at 23:40
  • @kingjeffrey I hate it when people downvote without leaving a comment... Apart from being a legitimate beginner question, it was well formed and clearly presented. Commented Jun 29, 2012 at 23:44

7 Answers 7

7

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));
}
Sign up to request clarification or add additional context in comments.

6 Comments

Clever! I like the way you think.
Using floating pointer for your iterator is a bad idea. It can lead to weird numbers and incorrect number of iterations. I hope you don't mind my fix.
This non-nested approach is useful if you want to limit the total number of lines independently of the number of message repeats. When the bound is a multiple of the number message repeats, a nested loop is clearer.
@ikegami, and more efficient, too. Increasing a long int is much faster than running a function that takes a double as argument and produces a double as result, passing it the result of a floating point operation and casting the result to a long int.
@ikegami's edits may have fixed issues in this answer, but it also made it significantly more opaque. I thought the original answer was clever, but with this fix, I prefer the nested loop solution.
|
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";
    }
}

2 Comments

+1 for the repetitive echos. It is bad form, but proves the need for the inner loop.
@kingjeffrey, Thanks. Nested loops can be daunting to newcomers, so I wanted to show the process that leads to using it instead of jumping to the answer.
3

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.

Comments

3

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"; 
    }
}

Comments

3

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.

Comments

2

you can make two loops

for($i = 1; $i <= 4; $i++) {
    for($j = 1; $j <= 4; $j++) {
        echo 'The number is '.$i."\n";
    }
}

Comments

1

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";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.