Check Odd Number in PHP

Beginner
⏱️ 7 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Parity

What You’ll Learn

An integer is odd when it is not divisible by 2 — in PHP, when $n % 2 != 0. This tutorial covers the modulo test, why zero is not odd, range listing, a live preview, worked PHP examples, edge cases, and complexity.

Definition

Not divisible by 2

Odd integers leave a nonzero remainder when divided by 2.

Modulo Test

n % 2 != 0

One remainder check classifies the number.

Zero

Even

0 is even, so isOdd(0) is false by design.

Range List

1..10

Print 1 3 5 7 9 from a simple loop.

Live Preview

Try values

Check 15, 0, or -3 under the same rule.

O(1) Check

O(k) range

One test is constant; scanning a range is linear.

Introduction

Odd numbers are the other half of the integers alongside evens. If splitting a pile into two equal whole rows leaves exactly one left over, the count is odd.

In PHP you detect that with the remainder of division by 2: $n % 2 != 0. Zero is even (0 = 2 · 0), so it correctly fails the odd test.

Why it matters?

Parity checks are classic interview and homework warm-ups: modulo, booleans, and the easy-to-miss fact that zero is even.

Key Highlights

Core Test

$n % 2 != 0

Zero Is Even

So it is not odd — by design.

Opposite of Even

isOdd and isEven partition the integers.

Step by Two

List odds with i += 2 after aligning start.

In short: return true when $n % 2 != 0; remember zero is even; reuse the same test inside range loops.

📝 Problem & Approach

Given an integer, decide whether it is odd using remainder modulo 2. Optionally list all odds in a closed range.

php
// 15 % 2 = 1  -> odd
// 14 % 2 = 0  -> not odd
// 0  % 2 = 0  -> not odd (even)

Inputs & Outputs

ItemTypeDescription
$numberintInteger to classify.
Returnbooltrue if odd under % 2 != 0.
Printed outputtextYes/no sentence, or listed odds in a range.

Minimal workflow

Pseudocode
function isOdd(n):
    return (n mod 2) != 0

function printOddsInRange(start, end):
    for i from start to end:
        if isOdd(i):
            output i

Method comparison

MethodIdeaNotes
Modulo$n % 2 != 0Clearest for beginners
Bit test($n & 1) != 0Common micro-optimization
Step-by-two listfor ($i = $oddStart; $i <= $end; $i += 2)Fewer iterations when listing odds

⚡ Quick Reference

GoalPattern
Is odd?return $n % 2 != 0;
Is even?return $n % 2 == 0;
Bit odd testreturn ($n & 1) != 0;
Print messageecho $number . " is an odd number.\n";
Filter in rangeif ($i % 2 != 0) echo $i . " ";
Step by twofor ($i = 1; $i <= 10; $i += 2)

📋 Modulo vs Bit Test vs Step-by-Two

Same odd numbers — different styles and trade-offs.

Modulo
% 2 != 0

This page — clearest interview default

Bit test
& 1

Fine later; mention after you know %

Step +2
i += 2

Lists odds without testing evens

Interview tip
mention 0

Zero is even — say it out loud

Context

When This Problem Shows Up

Reach for an odd check whenever parity or remainder-by-2 matters.

  1. Interview warm-ups

    Modulo, booleans, and the zero edge case.

  2. Sibling of even

    Flip != to == for the opposite check.

  3. Filtering ranges

    Print only odds (or skip them) in loops.

  4. Teaching %

    Remainder is easier to see with a leftover of 1.

  5. Not for floats

    Parity is an integer idea — cast or validate first.

Key benefit: one comparison that locks in modulo thinking and the even/odd partition of the integers.

🔮 Live Preview

Uses JavaScript safe integers. The rule matches the PHP idea: odd when n % 2 !== 0.

Try 0 (not odd), 7, or -3.

Live result
Press “Is it odd?” to see the verdict.

Examples Gallery

Three complete PHP programs — single odd check for 15, print odds in 1..10, and a step-by-two range list. Click View Output to reveal sample console results.

📚 Getting Started

A reusable helper and one sample value.

Example 1 — n % 2 != 0

Helper returns a boolean; sample value 15 with a clear yes/no line.

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

$number = 15;

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

How It Works

15 = 2 · 7 + 1, so the remainder is 1 and isOdd is true. Change $number to 14 or 0 to see the else branch.

⚡ List Odds in a Range

Reuse the same remainder test inside a for loop.

Example 2 — Odds in [1, 10]

Walk the closed range and print 1 3 5 7 9.

php
<?php
function printOddNumbersFrom1To10(): void
{
    echo "Odd numbers in the range 1 to 10:\n";
    for ($i = 1; $i <= 10; $i++) {
        if ($i % 2 != 0) {
            echo $i . " ";
        }
    }
    echo "\n";
}

printOddNumbersFrom1To10();
?>

How It Works

Each integer is tested once; only nonzero remainders are printed. Keep $i <= 10 so both endpoints stay inclusive.

⚙️ Skip the Evens

Advance by 2 after starting on an odd value.

Example 3 — Step-by-Two Odd Loop

Same printed odds as Example 2, with half as many loop iterations.

php
<?php
function printOddsStepByTwo(int $start, int $end): void
{
    if ($start % 2 == 0) {
        $start++;
    }

    echo "Odd numbers from $start toward $end (step 2):\n";
    for ($i = $start; $i <= $end; $i += 2) {
        echo $i . " ";
    }
    echo "\n";
}

printOddsStepByTwo(1, 10);
?>

How It Works

If $start is even, bump it to the next odd, then add 2 each time. You never visit an even integer in the loop body.

🧠 How the Algorithm Decides

1

Take an integer

Use a literal or a validated CLI value.

Input
2

Compute n % 2

If the remainder is not 0, the number is odd.

Test
3

Report clearly

Print a yes/no sentence, or list matching range values.

Output
=

Parity decided

The integer is labeled odd or not odd.

🔎 Worked Walkthrough — Sample Values

Apply $n % 2 != 0 to a few integers.

$n$n % 2Odd?
151Yes
140No
00No (even)
-5nonzeroYes

For nonnegative beginners, “remainder 1” is the usual mental picture of odd.

Use Cases

Where odd-number checks show up beyond the interview prompt.

1. Interview Warm-Ups

Modulo and boolean helpers.

Example: write isOdd($n).

2. Opposite of Even

Same idea with the flipped comparison.

Example: even uses == 0.

3. Range Filters

Print or skip odds in a loop.

Example: Example 2 pattern.

4. Teaching Remainder

Leftover 1 is easy to picture.

Example: 15 = 2·7 + 1.

5. Zero Edge Talk

Show that 0 is even, not odd.

Example: isOdd(0) is false.

6. Step Optimizations

List odds with i += 2.

Example: Example 3 pattern.

Pro Tip: open with “odd means remainder nonzero mod 2, and zero is even” before writing code.

Advantages

Why the modulo odd check works well in interviews.

  1. 1. One Clear Rule

    A single remainder comparison encodes the definition.

  2. 2. Easy to Flip

    Even is the same idea with == 0.

  3. 3. Composes Into Ranges

    Reuse isOdd inside any loop filter.

  4. 4. Constant Cost

    O(1) time and space for a single classification.

Pro Tip: keep isOdd boolean and put wording in the echo layer.

Usage Tips

Small habits that keep odd-number solutions interview-ready.

  1. 1. Prefer %

    Use modulo until bit tricks feel natural.

  2. 2. Mention Zero

    State that 0 is even before coding.

  3. 3. Keep Helpers Boolean

    Return true/false; print messages outside.

  4. 4. Inclusive Ranges

    Use <= when the prompt includes both ends.

  5. 5. Align Then Step

    For odd-only loops, bump even starts before += 2.

Pro Tip: dry-run 15, 14, and 0 aloud — those three catch almost every beginner mistake.

Common Pitfalls

Mistakes that commonly break odd-number solutions.

  1. 1. Calling Zero Odd

    Thinking 0 has no parity or is somehow special.

    → Zero is even; 0 % 2 == 0.

  2. 2. Using == 0 for Odd

    Copying the even test without flipping the comparison.

    → Odd needs != 0.

  3. 3. Exclusive Range Bounds

    Using < when the prompt includes the end value.

    → Prefer <= for closed ranges like 1 to 10.

  4. 4. Stepping Without Aligning

    Starting an even $start and adding 2 forever.

    → Bump even starts to the next odd first.

  5. 5. Applying Parity to Floats

    Passing decimals into an int helper silently.

    → Validate whole numbers on CLI paths.

Edge Cases

The phrase “not odd” includes evens and zero — do not confuse it with “even and positive.”

Zero

n = 0

0 % 2 == 0, so isOdd(0) is false.

Negatives

Signed integers

Odd negatives still satisfy n % 2 != 0 in PHP.

One

n = 1

Smallest positive odd integer.

Inclusive

i <= end

Keep both endpoints for “from 1 to 10.”

Even start

Step-by-two alignment

Bump even starts before adding 2.

Empty range

start > end

Loop never runs; print nothing.

⚖️ Facts Worth Knowing

Handy follow-ups interviewers sometimes ask.

  • Partition. Every integer is exactly one of even or odd.
  • Congruence. Odds are congruent to 1 mod 2; evens to 0.
  • Sum of two odds. Odd + odd is even; odd + even is odd.
  • LSB. On two’s complement, the least significant bit is 1 for odds.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Check three values

  • Run isOdd on 15, 0, and -3
  • Expect yes / no / yes

2. Print 1..20 odds

  • Reuse Example 2 with end = 20
  • Confirm ten printed odds

3. Step-by-two from 2

  • Call printOddsStepByTwo(2, 10)
  • Expect start bumped to 3

4. Write isEven

  • Flip the comparison to == 0
  • Assert isEven(n) == !isOdd(n)

Notes

  • Test: $n % 2 != 0 (or ($n & 1) != 0 once you are ready).
  • Zero is even, so it is not odd.
  • Ranges: reuse the same test inside a for loop.
  • Interview: mention that isOdd and isEven partition the integers.

Quick Takeaway: odd means $n % 2 != 0 — and zero is not odd.

⏱️ Time and Space Complexity

OperationTimeExtra space
isOdd(n)O(1)O(1)
Range [a, b]O(b - a + 1)O(1)
Step-by-two odd loopabout half as many iterationsO(1)

No heap allocation is required for these snippets.

Wrap Up

🎉 Conclusion

Checking an odd number is a one-line remainder test: return true when $n % 2 != 0. Reuse that helper for range listing, or step by two after aligning the start, and always remember that zero is even.

Practice the three examples above, then continue to palindrome numbers.

Odd means nonzero remainder mod 2 — say zero is even before you code.

💡 Best Practices

✅ Do

  • Use $n % 2 != 0 for clarity
  • State that zero is even
  • Keep isOdd boolean
  • Use inclusive bounds for closed ranges
  • Align even starts before stepping by 2

❌ Don’t

  • Call zero odd
  • Copy the even test without flipping
  • Apply parity to floats silently
  • Skip the 0 dry-run
  • Lead with bit tricks before modulo

Key Takeaways

Knowledge Unlocked

Five things to remember about odd numbers

Classify parity the interview-friendly way.

5
Core concepts
0 02

Zero

Even, not odd

Edge
? 03

Helper

isOdd bool

Code
04

Range

Filter or +2

Pattern
O 05

Cost

O(1) / O(k)

Analysis

❓ Frequently Asked Questions

It is a whole number that cannot be split into two equal whole piles without a leftover — dividing it by 2 leaves remainder 1 (for nonnegative integers). Examples: 1, 3, 15, 101.
No. Zero counts as even (you can write 0 = 2 · 0). So odd tests such as n % 2 != 0 correctly classify 0 as not odd.
The remainder when dividing by 2 is 0 for evens and nonzero for odds. For negatives, n % 2 != 0 still correctly means "not divisible by 2" for odd integers.
For integers, yes: exactly one of the two is true. In code, isOdd can be (n % 2 != 0) and isEven can be (n % 2 == 0).
On two's complement, the least significant bit is 1 for odd integers. It is a common micro-optimization; n % 2 != 0 is usually clearer for beginners.
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.
After aligning to the first odd, loop with i += 2 so you skip all evens.
Same modulo idea; even uses == 0 and odd uses != 0.

Did you Know? 🔊

Every whole number is either even or odd—never both, never neither. Zero is even, so the test n % 2 != 0 correctly says zero is not odd.

Continue to Palindrome Number

Learn how to check whether a number reads the same forwards and backwards in PHP.

Palindrome number tutorial →

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