Get remainder only from a division using PHP

Get remainder only from a division using PHP

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

Stefan Bogdanescu

Founder & Senior Architect

The Modulo Operator in PHP: Extracting Remainders Efficiently

In PHP, extracting the remainder from a division operation is straightforward because the language provides a dedicated operator for this exact purpose. While standard division (/) returns a floating-point result representing the quotient, it discards the information regarding what was left over after the largest possible integer multiple of the divisor was subtracted from the dividend. To retrieve that leftover value, you must use the modulo operator (%).

Consider the specific example provided in your query: dividing 19 by 5. If you execute $result = 19 / 5;, PHP returns 3.8. This is mathematically correct for division, but it fails to tell you that 3 fits into 19 five times with a remainder of 4. The standard division operator performs truncation towards zero (for integers) or generates a float, effectively hiding the remainder unless you manually implement logic to isolate it.

To get the remainder only, use the modulo operator like this:

$remainder = 19 % 5;
echo $remainder; // Outputs: 4

This is the idiomatic and most performant solution in PHP. The % symbol acts as a synonym for "modulo," which specifically calculates the remainder after division of two numbers. Unlike fmod(), which always returns a float even when both operands are integers, the % operator preserves integer types if both inputs are integers, making it preferable for scenarios involving discrete counting, hash indexing, or pagination calculations where type consistency matters.

Handling Negative Numbers and Edge Cases

While the modulo operator works perfectly for positive integers, PHP's behavior with negative numbers differs slightly from mathematical definitions in other languages like Python or JavaScript (which often use floor division logic). In PHP, the sign of the result follows the sign of the dividend (the number being divided), not the divisor.

For example:

echo -19 % 5; // Outputs: -4
echo 19 % -5; // Outputs: 4
echo -19 % -5; // Outputs: -4

If your application requires a mathematical modulo that always returns a positive remainder (e.g., 0 to n-1), you may need to adjust the result manually if negative inputs are possible. A common adjustment pattern is (a % n + n) % n, though understanding PHP's specific behavior helps avoid unnecessary complexity unless your logic explicitly demands it.

Alternatives: When Not to Use %

Sometimes developers confuse the modulo operator with the fmod() function. The fmod() function follows IEEE 754 floating-point arithmetic rules and always returns a float. If you are dealing with currency calculations or high-precision requirements where floating-point errors are unacceptable, sticking to integers and the % operator is safer. However, if one operand is already a float, fmod() might be appropriate, but remember that it will not return an integer type.

Another potential pitfall is assuming the modulo operator works on strings or mixed types without coercion. PHP performs implicit type juggling, so passing a string like "19" to % with an integer 5 will automatically cast the string to an integer. However, passing a non-numeric string like "abc" to % will result in a warning and treat the value as 0, which could silently break your logic if not handled.

Practical Applications in Laravel and PHP Ecosystems

In larger PHP frameworks like Laravel, you often encounter scenarios requiring modulo logic, such as cycling through arrays or implementing pagination offsets. While you can use native PHP syntax, ensuring consistency is key. If you are building a package or writing reusable code, consider leveraging the LaravelCompany resources to find community-tested patterns for handling arithmetic operations safely within your specific stack.

Ultimately, if your goal is simply to get the remainder only from a division, 19 % 5 is the definitive answer. It is concise, readable, and optimized by the PHP engine. Avoid reinventing the wheel with custom functions unless you need to handle specific edge cases regarding negative numbers or floating-point precision that the standard operator does not cover out of the box.

Our network