Home > Net >  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

Time:01-09

your text

The above warning is displayed in PHP 8.1 after upgrading from PHP 7.4

Any ideas on how this code can be changed for PHP 8.1 compatibility?

   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;
    }
   

CodePudding user response:

You should check your $strRawText to ensure it's not null before passsing to strlen(). This can be done either with explicit check or adding typehint.

You can also alternatively use null coalescing; which is more concise.

while ($iCharPos < strlen($strRawText ?? '')) ...
  • Related