0

The code below worked correctly in PHP 7.0, but after upgrading to 7.3 it now gives this warning:

PHP Warning: Illegal string offset on line 71

// Loop through routes
$array_result = "";
$array_index_result = "";
foreach($routes as $key => $route){

    $route_parts = explode("/", $route);
    $index = 0;
    $match = TRUE;

    // Reset array_result
    $array_result = "";

    foreach($route_parts as $route_part)
    {
        if(substr($route_part,0,1) != "$"){
            if(isset($url_parts[$index])){
                if($route_parts[$index]!=$url_parts[$index]) $match = FALSE;
            }else{
                $match = FALSE;
            }
        }else{
            if(isset($url_parts[$index])){
                $array_result[substr($route_parts[$index],1)] = $url_parts[$index];
            }
        }
        if(isset($url_parts[$index])){
            $array_index_result[$index] = $url_parts[$index];
            $index++;
        }
    }

    if($match && (count($route_parts) == count($url_parts)))
    {
        $Info = new ModRewriteInfo($array_result, $array_index_result);
        return $Info;
    }

}

// No match found
return FALSE;

Line 71 is

$array_result[substr($route_parts[$index],1)] = $url_parts[$index];

The code runs on PHP 7.0 but not in PHP 7.3. Why is that?

Don't Panic
  • 41,125
  • 10
  • 61
  • 80

1 Answers1

1

$array_result is a string.

$array_result = "";

In PHP 7.0 and before, assigning to an index of an empty string with [] would result in an array. From 7.1 onward, it stays a string and you get that warning.

See the documentation for backward incompatible changes in PHP 7.1: Assignment via string index access on an empty string

From the context of the rest of the code, it doesn't look like that variable should ever be a string, so you should initialize it with $array_result = []; instead.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80