How to increment a number by 2 in a PHP For Loop [duplicate] How to increment a number by 2 in a PHP For Loop [duplicate] php php

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


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"


<?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


You should use other variable:

 $m=0;  for($n=1; $n<=8; $n++):   $n = $n + $m;  $m++;  echo '<p>'. $n .'</p>'; endfor;