skip to Main Content

I can’t figured out whats wrong with this.

PHP 8.1
PHP Deprecated: Automatic conversion of false to array is deprecated

        $request = parse_url( $_SERVER['REQUEST_URI'] );
        $request['path'] = ( ! empty( $request['path'] ) ) ? $request['path'] : '';

2

Answers


  1. Chosen as BEST ANSWER

    Fixed my own question.

    $request = parse_url( rawurldecode( $_SERVER['REQUEST_URI'] ) );
    

  2. This pattern is most likely a bug:

    $foo = false;
    $foo['name'] = 'Jimmy';
    

    So PHP now warns about it:

    Deprecated: Automatic conversion of false to array is deprecated

    … and it’s likely that the code will fully stop working in future PHP versions.

    In your case, the false is coming from parse_url():

    On seriously malformed URLs, parse_url() may return false.

    Inspect $_SERVER['REQUEST_URI'] to figure out why PHP fails to parse it as URL. Use a step debugger of, alternatively, var_dump($_SERVER['REQUEST_URI']).

    On the other side, it isn’t entirely clear (not at least without further context) what the goal is. REQUEST_URI is going to have two have at most two components, path and query, and only query is optional. The code fragment does not really do anything, unless you run the script in command-line, in which case it’ll throw the Undefined array key "REQUEST_URI" warning. It can only do something different if you overwrite $_SERVER['REQUEST_URI'] at an earlier stage.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search