Check Even Number in PHP

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 2 Code Examples
Parity

What you’ll learn

  • The definition of even (including zero) and the usual PHP test $n % 2 === 0.
  • A tiny isEven helper plus printing evens in a closed interval [start, end].
  • Live preview and notes on parity for negative values.

Overview

Parity splits integers into evens and odds. The simplest PHP predicate is $number % 2 === 0, which returns a boolean you can pass to if or reuse in loops.

Two programs

One value (10) and a range 1–10 listing.

Live preview

Safe-range integers; shows parity with the same rule as the PHP samples.

Rigor

Explicit note on zero and negative values.

Prerequisites

The modulo operator %, if, and for loops.

  • Basic PHP syntax, functions, and echo.
  • Optional: bitwise & as an alternative parity test.

What is an even number?

An integer n is even iff n = 2k for some integer k. On the number line, evens are spaced by two: …, -4, -2, 0, 2, 4, ….

Checking divisibility by 2 is the standard beginner task and appears in many loops and filters.

Even n % 2 == 0
Odd n % 2 != 0
Zero even

Congruence mod 2

Even integers are exactly those congruent to 0 modulo 2: n ≡ 0 (mod 2).

10

10 = 2 * 5, remainder 0 upon division by 2.

Intuition

8 Even
Pairs
four groups of two
7 Odd
Remainder
7 % 2 = 1

Takeaway: parity is one of the first modular filters used in interview programs.

Live preview

Integers in the JavaScript safe range. Uses n % 2 === 0 (same idea as PHP code).

Try 0, -4, or 11.

Live result
Press “Check parity” to classify.

Algorithm

Goal: return true iff n is divisible by 2.

Compute n % 2

If remainder is 0, n is even; otherwise odd.

Range scan

For each i in [start, end], apply same predicate and print matches.

📜 Pseudocode

Pseudocode
function isEven(n):
    return (n mod 2) = 0

function printEvensInRange(start, end):
    for i from start to end:
        if isEven(i):
            output i
1

$n % 2 === 0

Minimal helper returning a boolean, same parity logic as the C reference.

php
<?php
function isEven(int $number): bool
{
    return $number % 2 === 0;
}

$number = 10;

if (isEven($number)) {
    echo "{$number} is an even number.\n";
} else {
    echo "{$number} is not an even number.\n";
}
?>

Explanation

isEven returns true or false, which is directly usable in if.

2

Evens in [1, 10]

Same output as the C range demo: 2 4 6 8 10.

php
<?php
function isEven(int $num): bool
{
    return $num % 2 === 0;
}

function checkEvenNumbersInRange(int $start, int $end): void
{
    echo "Even numbers in the range {$start} to {$end}:\n";

    for ($i = $start; $i <= $end; $i++) {
        if (isEven($i)) {
            echo $i . " ";
        }
    }

    echo "\n";
}

checkEvenNumbersInRange(1, 10);
?>

Explanation

The loop checks each integer in an inclusive range. You can also use ($i & 1) === 0 as an alternative.

Optimization

Step by two. If only evens are needed, align start to even and increment by 2.

Bitwise parity. ($n & 1) === 0 is a valid alternative for integers.

Interview: mention 0 is even and then show modulo test first.

❓ FAQ

An integer n is even if there exists an integer k with n = 2k. Equivalently, n is divisible by 2 with remainder 0.
Yes: 0 = 2 * 0. The test $n % 2 === 0 is true for $n = 0 in PHP.
The remainder of division by 2 is 0 for even integers and nonzero for odd integers. It is the clearest parity test for beginners.
Yes. ($n & 1) === 0 also detects even integers. It is a common alternative; modulo is more readable for learning.
Even negatives like -4 still satisfy $n % 2 === 0. Odd negatives return a nonzero remainder.
A single test is O(1) time and O(1) space. Scanning a range [a, b] is O(b - a + 1) with O(1) extra space.

🔄 Input / output examples

Change $number in Example 1 or $start/$end in Example 2.

nEven?
0Yes
10Yes
11No
-4Yes

Edge cases and pitfalls

Treating “not even” as “odd” is correct for integers. Always confirm input is integer when reading from user input.

Zero

n = 0

Even by definition; 0 % 2 === 0 is true.

Negative

Negative values

-4 is even. The sign does not change parity.

Types

Float input

Convert and validate as integer before running parity checks.

Off-by-one

Inclusive range

Use $i <= $end when the task says include end value.

⏱️ Time and space complexity

OperationTimeExtra space
isEven(n)O(1)O(1)
Range [a, b]O(b - a + 1)O(1)
Step-by-two loopO((b-a)/2) iterationsO(1)

No auxiliary memory is required beyond loop indices.

Summary

  • Test: $n % 2 === 0 or ($n & 1) === 0.
  • Zero is even; range scans reuse the same predicate.
  • Watch-outs: integer validation and inclusive loop bounds.
Did you know?

In modern mathematics zero is even: it is an integer multiple of 2 (0 = 2 · 0). The expression $n % 2 === 0 in PHP is true for $n = 0.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful