Search code examples
phpphp-8

parse_str syntax errors after upgrading from PHP 7.4 to 8.0


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?


Solution

  • 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);