0

Im not a php expert at all, and I am looking for simple php loop that will add 1 to every third number

This should look like this

111 222 333 444 555 666 777

and it will repeat 20 times.

Thank you for your help in advance!

2
  • After 9, like 101010? Commented Sep 7, 2013 at 8:20
  • 111 222 333 444 555 666 777 888 999 101010. Thank you Commented Sep 7, 2013 at 8:21

4 Answers 4

2

Try this one:

for($i=1; $i <= 20; $i++ )
{
    echo $i.$i.$i."<br />";
}
Sign up to request clarification or add additional context in comments.

2 Comments

I dont down you but , , What you do if he wants 10times then you will add $i.$i.$i.$i.$i.$i.$i.$i.$i.$i..??
I used $i.$i.$i because of it's just three time. If he wanted 10 times, i would make with nested for or function or another thing. No need to force for 3 times ;)
0

Try like

$str = '';
for($i = 1 ; $i <= 20 ; $i++) {
    for($j = 0 ; $j < 3 ; $j++) {
         $str .= $i;
    }
    $str .= " ";
}
echo $str;

2 Comments

You mean for instead of foreach?
Your result like 11 22 33 44 55 66 77. Questioner want third number
0

Use

<?php

$i = 0;
$str = "";
while(++$i<=90) {
    $str .= $i.$i.$i; 
    $str .= " ";    
}
echo $str;

?>

Comments

0

better approach to use str_repeat

click here to look at demo

 for($i=1; $i <= 20; $i++ ){
    echo str_repeat($i, 4), '<br />';
 }

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.