How to count number of digit included zero in php

How to count number of digit included zero in php

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

Stefan Bogdanescu

Founder & Senior Architect

Counting Digits Including Leading Zeros in PHP

Counting digits in a numeric value often seems straightforward, but the moment leading zeros enter the picture, standard type casting fails you. A common pattern seen in legacy databases or CSV exports is storing numbers as fixed-width strings like 00123. If you treat this purely as an integer in PHP, the leading zeros vanish immediately during type conversion.

Consider a function designed to count digits:

function count_digit($number) {
    return strlen((string) $number);
}

When you pass $number = 123 to this function, it correctly returns 3. However, if your input is $num = '00123', the behavior changes depending on how PHP interprets that value. If passed as a string directly, (string) casting might seem harmless, but if the variable holds an integer value (e.g., $num = 123), the zeros are already lost before you even attempt to count them.

The core issue arises from how PHP handles numeric strings versus integers. When you define a variable like $num = '00123', it is technically a string containing characters. However, if your data source treats this as a number and performs implicit conversion or arithmetic operations, the value becomes 123. Once that happens, strlen((string) $number) returns 3 instead of the desired 5 for a five-digit field like 00123.

To solve this, you must preserve the original string representation before any numeric coercion occurs. The most robust approach is to ensure your function operates on the raw input without triggering integer parsing. If the input is guaranteed to be a string, simply passing it directly to strlen works:

function count_digit($value) {
    // Ensure we are working with a string, not an integer
    if (is_numeric($value)) {
        // If the value looks like a number but might have lost zeros via casting,
        // check if it's actually a string in memory first.
        return strlen((string)$value);
    }
    
    // If $value is already a string '00123', this works directly:
    return strlen($value); 
}

However, relying on type casting alone is brittle. The real fix lies in understanding that if your database stores 00123 as an integer, the zeros are gone before they hit your PHP code. You must store such values as strings or handle them with formatting functions that reconstruct the original look. Using str_pad can simulate leading zeros if the length is known:

function count_digit($value, $minLength = 5) {
    // Convert to string if it isn't already
    $stringVal = (string)$value;
    
    // If the string representation is shorter than expected due to lost zeros,
    // pad it back up to the known length. This assumes you know the total digit count
    // from the database schema or a specific requirement.
    if (strlen($stringVal) < $minLength) {
        return strlen(str_pad($stringVal, $minLength, '0', STR_PAD_LEFT));
    }
    
    return strlen($stringVal);
}

$count = count_digit('123', 5); // Returns 5

In this implementation, we explicitly reconstruct the zero-filled string using str_pad only when the length is insufficient. This avoids the overhead of padding every single number if it already has the correct digit count. It also handles cases where the input is an integer that lost its zeros during storage or transmission.

For applications built on the Laravel framework, you might encounter this issue when validating request inputs or processing Eloquent casts. If a column in your database is defined as string and contains values like 00123, ensure your model cast does not convert it to an integer inadvertently. If you are using a numeric field that requires zero-padding for display or counting purposes, consider defining the column as a string type in the migration rather than treating it as a pure number.

Ultimately, the decision to treat these values as strings versus integers depends on your data integrity requirements. If the leading zeros carry semantic meaning—such as an account ID or a serial number—they must never be treated as numeric types. Always validate input types and preserve the original string format when counting digits that include leading zeros.

Our network