W3docs

How to increment a number by 2 in a PHP For Loop

You can increment a number by 2 in a PHP for loop by using the increment operator (++) and adding 2 to it on each iteration.

You can increment a number by 2 in a PHP for loop by using the increment operator (++) and adding 2 to it on each iteration. Here is an example of a for loop that starts at 0, ends at 10, and increments by 2 on each iteration:

Example of incrementing a number by 2 in a PHP for loop

<?php

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

This will output:


0 2 4 6 8 10

Another way you can increment the number by 2 is using the $i = $i + 2 instead of $i += 2

Example of incrementing a number by 2 in PHP

<?php

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

This will also output:


0 2 4 6 8 10