parse_str() Function Use in PHP 8.4 & 8.5

In PHP 8.4 and 8.5, the parse_str() function itself has no new changescompared to PHP 8.0+. Its core behavior remains: parsing a URL query string into an array, and the second parameter $result is mandatory(since PHP 8.0). The main difference lies in PHP 8.5 introducing a new URI extension, which offers a more modern alternative for URL parsing, but parse_str() itself still works as usual.

Core Usage (PHP 8.4 & 8.5 Identical).

The only recommended way to call it is passing the result array (since PHP 8.0, omitting the second parameter will result in a fatal error) :

$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str, $output);

echo $output[ first ];    // value
echo $output[ arr ][0];   // foo bar
echo $output[ arr ][1];   // baz

Behaviour to Note.

  • Key name conversion: Spaces and dots in parameter names are converted to underscores (e.g., My Value becomes My_Value), which applies to the array keys as well.
  • Automatic URL decoding: All values are automatically decoded using urldecode() rules (e.g., + becomes a space) .
  • max_input_vars limit: Affected by the max_input_vars directive (default 1000); exceeding the limit triggers an E_WARNING, and excess variables are discarded.

PHP 8.5 Related New Feature (Not a parse_str Change).

PHP 8.5 introduces a new URI extension (UriRfc3986Uri class), which can replace parse_url() for more standardized URL parsing, but parse_str() itself has not been modified. You can use it in combination:

use UriRfc3986Uri;

$uri = new Uri( https://example.com/?abc=abc );
$query = $uri->getQuery();  // "abc=abc"
parse_str($query, $params);  // [ abc => abc ]

7 Views