- Pairs
- four groups of two
Check Even Number in PHP
What you’ll learn
- The definition of even (including zero) and the usual PHP test
$n % 2 === 0. - A tiny
isEvenhelper 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.
Congruence mod 2
Even integers are exactly those congruent to 0 modulo 2: n ≡ 0 (mod 2).
1010 = 2 * 5, remainder 0 upon division by 2.
Intuition
- 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).
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
function isEven(n):
return (n mod 2) = 0
function printEvensInRange(start, end):
for i from start to end:
if isEven(i):
output i$n % 2 === 0
Minimal helper returning a boolean, same parity logic as the C reference.
<?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.
Evens in [1, 10]
Same output as the C range demo: 2 4 6 8 10.
<?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
🔄 Input / output examples
Change $number in Example 1 or $start/$end in Example 2.
| n | Even? |
|---|---|
0 | Yes |
10 | Yes |
11 | No |
-4 | Yes |
Edge cases and pitfalls
Treating “not even” as “odd” is correct for integers. Always confirm input is integer when reading from user input.
n = 0
Even by definition; 0 % 2 === 0 is true.
Negative values
-4 is even. The sign does not change parity.
Float input
Convert and validate as integer before running parity checks.
Inclusive range
Use $i <= $end when the task says include end value.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
isEven(n) | O(1) | O(1) |
Range [a, b] | O(b - a + 1) | O(1) |
| Step-by-two loop | O((b-a)/2) iterations | O(1) |
No auxiliary memory is required beyond loop indices.
Summary
- Test:
$n % 2 === 0or($n & 1) === 0. - Zero is even; range scans reuse the same predicate.
- Watch-outs: integer validation and inclusive loop bounds.
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.
8 people found this page helpful
