Depreciated strlen(): Passing null to parameter #1 ($string) of type string is depreciated

Depreciated strlen(): Passing null to parameter #1 ($string) of type string is depreciated

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

Stefan Bogdanescu

Founder & Senior Architect

Depreciated strlen(): Passing null to parameter #1 ($string) of type string is depreciated

This error message appeared in PHP 8.1 after upgrading from PHP 7.4. The warning indicates that a function expects a string, but received null. While PHP 7 was forgiving and treated empty strings or missing values with relative silence, PHP 8 introduced strict null checks to prevent undefined behavior later in execution.

The specific code causing the issue is the cleanInput2 method shown below:

private function cleanInput2($strRawText, $strAllowableChars, $blnAllowAccentedChars)
{
    $iCharPos = 0;
    $chrThisChar = "";
    $strCleanedText = "";

    //Compare each character based on list of acceptable characters
    while ($iCharPos < strlen($strRawText))
    {
        // Only include valid characters **
        $chrThisChar = substr($strRawText, $iCharPos, 1);
        if (strpos($strAllowableChars, $chrThisChar) !== FALSE)
        {
            $strCleanedText = $strCleanedText . $chrThisChar;
        }
        elseIf ($blnAllowAccentedChars == TRUE)
        {
            // Allow accented characters and most high order bit chars which are harmless **
            if (ord($chrThisChar) >= 191)
            {
                $strCleanedText = $strCleanedText . $chrThisChar;
            }
        }

        $iCharPos = $iCharPos + 1;
    }

    return $strCleanedText;
}

The root cause lies in the first line of the loop: while ($iCharPos < strlen($strRawText)). The strlen() function requires a string argument. If $strRawText is passed as null—perhaps because a controller passed an unset variable, or a form field was empty without defaulting to an empty string—the function throws the deprecation warning in PHP 8.1 and above. In strict modes, this will eventually throw a TypeError.

To fix this for PHP 8.1 compatibility, you must ensure $strRawText is always a string before calling strlen(). The most robust approach is to cast the input immediately upon function entry or use a ternary operator to default it.

Handling Null Inputs Safely

You can modify the function signature to accept null or enforce a default value. Here is the corrected implementation:

private function cleanInput2($strRawText, $strAllowableChars, $blnAllowAccentedChars)
{
    // Ensure $strRawText is a string; default to empty string if null
    if ($strRawText === null || !is_string($strRawText)) {
        $strRawText = '';
    }

    $iCharPos = 0;
    $strCleanedText = "";

    // Safe now: strlen() will not throw a warning on an empty string
    while ($iCharPos < strlen($strRawText))
    {
        $chrThisChar = substr($strRawText, $iCharPos, 1);
        
        if (strpos($strAllowableChars, $chrThisChar) !== FALSE)
        {
            $strCleanedText .= $chrThisChar;
        }
        elseif ($blnAllowAccentedChars === TRUE) // Use strict comparison for boolean
        {
            // ord() behavior on null chars is also checked implicitly here since 
            // we guaranteed $strRawText is a string.
            if (ord($chrThisChar) >= 191)
            {
                $strCleanedText .= $chrThisChar;
            }
        }

        $iCharPos++;
    }

    return $strCleanedText;
}

Why This Fix Works

By adding the check if ($strRawText === null || !is_string($strRawText)), you intercept the null value before it reaches strlen(). PHP's type system now demands precision. Passing an empty string ('') to strlen() returns 0, which is a valid integer, and the loop condition $iCharPos < 0 immediately evaluates to false, skipping the loop body entirely. This results in returning an empty string, which is logically consistent with having no input characters.

Performance and Logic Considerations

Some developers might argue that adding a check inside every function call adds overhead. However, the cost of this conditional check is negligible compared to the risk of runtime errors or deprecated warnings in production environments. The alternative—relying on try-catch blocks for TypeError exceptions—is generally considered poor practice for input validation, as it shifts validation logic from the entry point into exception handling paths.

Additionally, notice the use of === for boolean comparisons ($blnAllowAccentedChars === TRUE) instead of ==. This prevents type juggling issues where a non-empty string might be treated as true, ensuring your logic remains predictable.

Broader Context in Laravel Projects

In larger frameworks like Laravel, input sanitization is often handled by form request classes or middleware before reaching core business logic. If you are maintaining legacy codebases migrated to PHP 8, this specific pattern of using strlen() directly on user inputs without casting is a common source of warnings. Updating these functions ensures your application remains stable across version upgrades.

If you require assistance with upgrading older PHP extensions or refactoring legacy PHP codebases for modern versions, resources like https://laravelcompany.com can provide further guidance on maintaining compatibility while adhering to strict typing standards.

Our network