How to increment a number by 2 in a PHP For Loop [duplicate]
Asked Answered
M

6

39

The following is a simplified version of my code:

<?php for($n=1; $n<=8; $n++): ?>
    <p><?php echo $n; ?></p>
    <p><?php echo $n; ?></p>
<?php endfor; ?>

I want the loop to run 8 times and I want the number in the first paragraph to increment by 1 with each loop, e.g.

1, 2, 3, 4, 5, 6, 7, 8 (this is obviously simple)

However, I want the number in the second paragraph to increment by 2 with each loop, e.g...

1, 3, 5, 7, 9, 11, 13, 15

I can't figure out how to make the number in the second paragraph increment by 2 with each loop. If I change it to $n++ then it increments by 2, but it then makes the loop run only 4 times instead of 8.

Any help would be much appreciated. Thanks!

Marnamarne answered 7/11, 2013 at 8:48 Comment(2)
...bit harsh, this is the first time I've worked with for loops.Marnamarne
@Alma Do: Why mock the OP, maybe the guy is new? No all people are code-experienced like you. What's sad is your comment and those who upvoted it.Joey
C
22
<?php
  for ($n = 0; $n <= 7; $n++) {
    echo '<p>'.($n + 1).'</p>';
    echo '<p>'.($n * 2 + 1).'</p>';
  }
?>

First paragraph:

1, 2, 3, 4, 5, 6, 7, 8

Second paragraph:

1, 3, 5, 7, 9, 11, 13, 15
Carpetbagger answered 7/11, 2013 at 8:50 Comment(1)
Thanks, exactly what I needed. This is obviously very simple as suggested by the downvotes/comments on the original question, but a couple of the other answers offered are either plain wrong or unnecessarily complicated. Not immediately obvious for a beginner.Marnamarne
E
101

You should do it like this:

 for ($i=1; $i <=10; $i+=2) 
{ 
    echo $i.'<br>';
}

"+=" you can increase your variable as much or less you want. "$i+=5" or "$i+=.5"

Elboa answered 15/6, 2016 at 22:47 Comment(2)
Yep, this doesn't answer the question correctly. It only points out a feature of the language which fits better in a comment, if that's the purpose.Shea
I agree with the criticism of the incompleteness of the question, but that raises a question for me. This answer DOES answer the question in the title, which is what I googled for. This may not be the most complete answer, but it's by far the most popular. So is it still inappropriate even if it's useful?Astronomical
C
22
<?php
  for ($n = 0; $n <= 7; $n++) {
    echo '<p>'.($n + 1).'</p>';
    echo '<p>'.($n * 2 + 1).'</p>';
  }
?>

First paragraph:

1, 2, 3, 4, 5, 6, 7, 8

Second paragraph:

1, 3, 5, 7, 9, 11, 13, 15
Carpetbagger answered 7/11, 2013 at 8:50 Comment(1)
Thanks, exactly what I needed. This is obviously very simple as suggested by the downvotes/comments on the original question, but a couple of the other answers offered are either plain wrong or unnecessarily complicated. Not immediately obvious for a beginner.Marnamarne
E
1

You should use other variable:

 $m=0; 
 for($n=1; $n<=8; $n++): 
  $n = $n + $m;
  $m++;
  echo '<p>'. $n .'</p>';
 endfor;
Estrellaestrellita answered 7/11, 2013 at 8:51 Comment(1)
Indeed, nice. I can't stand unnecessary php tagsHalvah
D
1

Another simple solution with +=:

$y = 1;

for ($x = $y; $x <= 15; $y++) {
  printf("The number of first paragraph is: $y <br>");
  printf("The number of second paragraph is: $x+=2 <br>");
} 
Defrayal answered 19/11, 2017 at 21:38 Comment(0)
L
0

Simple solution

<?php
   $x = 1;
     for($x = 1; $x < 8; $x++) {
        $x = $x + 1;
       echo $x;
     };    
?>
Leucocratic answered 21/12, 2016 at 12:37 Comment(1)
First $x = 1; is useless thereCollenecollet
R
-5
<?php    
     $x = 1;

     for($x = 1; $x < 8; $x++) {
       $x = $x + 2;
       echo $x;
     };
?>
Richert answered 7/11, 2013 at 9:32 Comment(1)
this is wrong on so many levels... first, it will output: 369, $x is unnecessarily declared twice, semicolon after curly bracket doesn't do anything };, closing php tag us useless too ?>Casilde

© 2022 - 2024 — McMap. All rights reserved.