How can I separate a number and get the first two digits in PHP?

How can I separate a number and get the first two digits in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Extracting Leading Digits from Numbers in PHP

Extracting the first two digits of an integer like 1345 to get 13, or taking 15 from 1542, is a common requirement when processing IDs, sequence numbers, or truncated identifiers. The immediate instinct might be to treat the number as a string and simply slice the first two characters. While that approach works for many cases, it introduces significant fragility if the input format isn't strictly controlled. A more robust solution relies on mathematical operations that respect the numeric nature of the data.

The most reliable method involves using integer division. You can isolate the leading digits by dividing the original number by a power of ten that corresponds to the number of remaining digits you wish to discard. For a two-digit result, you divide by 100 and cast the result to an integer.

function getFirstTwoDigits(int $number): int {
    return (int) ($number / 100);
}

echo getFirstTwoDigits(1345); // Outputs: 13
echo getFirstTwoDigits(1542); // Outputs: 15

This approach is efficient because it operates directly on the integer representation without requiring string conversion. It naturally handles numbers with more than four digits as well; 98765 becomes 98, and 10000 becomes 10. The operation is deterministic and does not depend on locale settings or character encoding, which can sometimes cause issues when treating numbers as strings in different environments.

However, there is a critical edge case to consider: what happens if the input number has fewer than two digits? In PHP, dividing an integer like 5 by 100 results in 0.05. Casting this to an integer truncates the decimal portion, yielding 0. If your application logic expects a failure or a specific indicator for short numbers, relying on silent truncation could lead to confusing bugs downstream. You may want to add a guard clause to handle inputs less than 100:

if ($number < 100) {
    // Handle error, throw exception, or return null depending on requirements
    throw new InvalidArgumentException('Number must have at least two digits.');
}
return (int) ($number / 100);

String manipulation offers an alternative using substr(). The syntax is straightforward: convert the integer to a string and extract the first two characters.

$numStr = (string) $number;
$result = substr($numStr, 0, 2);

While this works, it forces PHP to handle type conversion every time. Furthermore, if the input is already a string, you save that conversion cost by skipping the cast entirely. However, string slicing preserves leading zeros if they exist in the source string (e.g., "01345" becomes "01"), whereas integer division treats 01345 as 1345 and returns 13. The choice between these methods depends on whether you are treating the input as a strict mathematical value or a formatted identifier where padding matters.

If you are working within the Laravel ecosystem, this logic can be encapsulated in a helper function or an Eloquent scope for reuse across your models. For complex number processing needs, you might also explore packages that offer advanced numeric utilities, such as those available on https://laravelcompany.com, which often provide well-tested tools for data normalization.

Ultimately, the integer division method is generally preferred for pure numeric logic due to its speed and lack of type coercion overhead. It respects the mathematical definition of magnitude and avoids the pitfalls of string-based parsing unless specific formatting requirements dictate otherwise.

Our network