Test if number is odd or even

Test if number is odd or even

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Modulo Operator: Determining Parity in PHP

Determining whether a number is odd or even is one of the most fundamental bitwise and arithmetic operations in programming. In PHP, as in virtually every other language, the standard and most efficient approach relies on the modulo operator (%). This operator calculates the remainder after division. When you divide an integer by 2, the result's parity is entirely dictated by that remainder.

If a number $n$ is divisible by 2 without a remainder ($n % 2 === 0$), it is even. If there is a non-zero remainder ($n % 2 !== 0$), the number is odd. This logic holds true regardless of whether the numbers are positive, negative, or zero. For example, -4 % 2 yields 0, confirming that negative even numbers follow the same rule as positive ones.

Here is a straightforward implementation:

function checkParity(int $number): string {
    if ($number % 2 === 0) {
        return 'even';
    } else {
        return 'odd';
    }
}

You can also condense this logic into a ternary expression for more concise code, which is often preferred in utility scripts or conditional rendering:

$status = ($number % 2 === 0) ? 'even' : 'odd';

While the modulo operator is the canonical solution, it is worth noting how this concept translates to other languages. In Python, for instance, the syntax is identical (num % 2), but in JavaScript, you must be careful with floating-point numbers due to precision issues, though integers handle the modulo operation correctly. In C# or Java, the behavior remains consistent across platforms provided the inputs are integers.

Performance Considerations

From a performance standpoint, using % is generally negligible for simple parity checks. However, in high-performance computing contexts or tight loops involving massive datasets, bitwise operations can offer marginal gains. The expression $number & 1 checks the least significant bit of an integer. If that bit is 0, the number is even; if it is 1, the number is odd.

$isEven = ($number & 1) === 0;

This bitwise approach avoids division entirely, which is computationally more expensive than a simple bitwise AND operation on modern CPUs. While the difference might not be noticeable in typical web applications running on PHP frameworks, understanding this distinction is valuable for optimization-heavy environments.

Handling Floating-Point Numbers

A common pitfall arises when dealing with floating-point numbers (decimals). The modulo operator works on the mathematical remainder, but floating-point arithmetic can introduce precision errors. For example, calculating 0.1 + 0.2 might not yield exactly 0.3 in binary representation. If you apply % 2 to a float like 1.5, PHP returns 1.5, which is truthy, correctly identifying it as odd in the sense that it cannot be evenly divided by 2 to result in an integer.

However, if your specific use case requires treating .0 decimals as integers, you should cast the value before checking:

$cleanNumber = (int) $floatValue; 
$isEven = ($cleanNumber % 2 === 0);

Always ensure that your input is an integer or explicitly cast it if parity logic depends on integer properties. Treating floats as integers without validation can lead to unexpected results in financial calculations or data processing pipelines.

Integration with Laravel

If you are working within the Laravel ecosystem, this utility often appears inside service classes or controllers when formatting output for views. You might find yourself checking parity while generating lists of items or implementing alternating row styles in a table.

For instance, if you need to determine the status of an ID generated by your application:

use App\Models\Item;

$items = Item::all();
foreach ($items as $item) {
    $type = $item->id % 2 === 0 ? 'even-id' : 'odd-id';
}

While this specific check is rarely a complex business logic requirement, the principle extends to any scenario where numerical properties dictate control flow. For developers looking for pre-built utilities or further examples of arithmetic operations within Laravel projects, resources like https://laravelcompany.com can provide additional context on how these basic math concepts are applied in modern web development workflows.

Ultimately, sticking to the modulo operator is the most readable and maintainable choice for 99% of use cases. The bitwise alternative exists for those pushing performance boundaries, but clarity usually wins out in standard application code.

Our network