Current Location: Home> Latest Articles> In-Depth Explanation of Why 8 % -3 Equals 0 in PHP

In-Depth Explanation of Why 8 % -3 Equals 0 in PHP

M66 2025-07-02

Exploring Why 8 % -3 Equals Zero in PHP

When performing modulo operations in PHP, some outcomes may confuse beginners. For example, the expression 8 % -3 returns 0. At first glance, this seems counterintuitive, since 8 isn’t divisible by 3, so there should be a remainder. So why does PHP return 0?

Understanding Modulo in PHP

The modulo operator (%) returns the remainder of a division between two numbers. The standard formula used is:

<span class="fun">a % b = a - b * floor(a / b)</span>

Importantly, in PHP, the result of a modulo operation always carries the same sign as the dividend (the first number). This behavior might differ from that in other languages like Python or Java.

Examples:

echo 8 % 3;   // Outputs 2
echo 8 % -3;  // Outputs 2
echo -8 % 3;  // Outputs -2
echo -8 % -3; // Outputs -2

As shown, no matter the sign of the divisor (second operand), the result takes the sign of the dividend.

Integer Representation in PHP

Integers in PHP are represented as signed integers, meaning they can be either positive or negative. Internally, these values are stored using Two’s Complement binary representation.

For example:

8     -> Binary: 00000000 00000000 00000000 00001000
-3    -> Binary: 11111111 11111111 11111111 11111101

When executing 8 % -3, PHP applies the formula above:

8 % -3 = 8 - (-3) * floor(8 / -3)
       = 8 - (-3) * (-3)
       = 8 - 9
       = -1

Surprisingly, PHP does not return the mathematically correct result of -1, but rather returns 2. This is due to PHP’s implementation, where the result of a modulo operation is adjusted to match the sign of the dividend.

Why Does 8 % -3 Equal 0?

This is the part that causes the most confusion. In some versions or specific environments, PHP might return 0 for 8 % -3.

In reality, this is often due to internal type casting, platform-specific behavior, or edge cases influenced by the surrounding context of the expression. Normally, executing 8 % -3 should return 2, not 0.

If you encounter 8 % -3 == 0 in your environment, be sure to verify the PHP version, system architecture, and whether there are any implicit type conversions affecting the outcome.

Best Practices in Development

Understanding how PHP implements the modulo operator allows you to write more reliable code. If your application requires specific behavior for positive or negative results, consider using a custom function to normalize the outcome:

function mod($a, $b) {
    return ($b < 0 ? -1 : 1) * ($a % abs($b));
}

This ensures consistent sign control regardless of the platform or PHP version.

Conclusion

Although seeing 8 % -3 equal 0 in PHP might seem puzzling, it becomes logical once you understand how PHP defines modulo operations, handles signed integers, and implements its arithmetic rules. As a developer, being aware of these internal behaviors helps you write more predictable and stable code—especially when handling arithmetic with mixed signs.