-4

After updating to PHP8, my site has started crashing with the error

PHP message: PHP Fatal error: Uncaught ArgumentCountError: parse_str() expects exactly 2 arguments, 1 given

I believe it some change in the php 8 syntax, but I am not sure. I used to use php 7.4

The problematic line is:

$url_vars = parse_str($parse_url['query'] ?? 0);

The section of the problematic script is below:

        if ( $referrer !== FALSE && $referrer !== '' && stripos($referrer, '://') )
    {
            $parse_url      =       parse_url($referrer);
            $domain         =       $parse_url['host'];
            $query          =       $parse_url['query'] ?? 0;
            $dotdomain      =       '.'.$domain;

            if ( strlen($parse_url['query'] ?? 0) >= 1 )
            {
                    $url_vars = parse_str($parse_url['query'] ?? 0);

                    if (FALSE === empty($url_vars['q']))
                    {
                            $utm_keyword = $url_vars['q'] ?? 0;
                    }
            }

Can someone more familiar with php 8 see any obvious error on the code above?

Dharman
  • 30,962
  • 25
  • 85
  • 135
relima
  • 3,462
  • 5
  • 34
  • 53
  • 2
    Obvious error is obvious. See https://www.php.net/manual/en/function.parse-str.php especially where it says : `8.0.0 result is no longer optional.`. Move `$url_vars` inside of the function as the function doesn't return anything. – aynber Jul 21 '21 at 19:54

1 Answers1

2

As of PHP 8.0 the second parameter, result, is no longer optional and is required.

result should be the variable you want to place your output into.

You can view the changelog on the official parse_str documentation.

parse_str($parse_url['query'] ?? 0, $url_vars);
Kim Hallberg
  • 1,165
  • 1
  • 9
  • 18