How to Print Even Numbers in PHP (1 to 100)

even_numbers_formatted

Printing even numbers in PHP is a basic programming task that helps beginners understand loops and number operations. There are different methods to achieve this, but one of the simplest ways is by using a for loop.

Even numbers are numbers that are divisible by 2, such as 2, 4, 6, and so on. In this example, we will print even numbers between 1 and 100.

Method 1: Using For Loop

In this method, we use a for loop to print even numbers between 1 to 100. The loop starts from 2, which is the first even number. Then, instead of increasing by 1, the loop increments by 2 each time. This ensures that only even numbers are printed.

PHP Code Example

<?php
echo "Even numbers between 1 to 100 :- ";
for ($j = 2; $j <= 100; $j += 2){
    echo $j . ", ";
}
?>

add_number_series

Explanation

  • The loop starts at 2 (first even number)
  • The condition $j <= 100 ensures numbers stay within range
  • The increment $j += 2 skips odd numbers
  • Each number is printed with a comma for readability

Conclusion

Using a for loop is one of the easiest ways to print even numbers in PHP. This method is efficient and avoids unnecessary checks, making it ideal for beginners learning PHP programming.

Leave a Reply