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
Fixed my own question.
This pattern is most likely a bug:
So PHP now warns about it:
… 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():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
andquery
, and onlyquery
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.