parse_str()

parse_str() — Parse a string as a URL query string.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Parses string as if it were the query string passed via a URL and sets keys in the provided result array. If no result is passed, values are instead set as variables in the current scope.

Signature

parse_str(string $string, array &$result): void

Parameters

ParameterTypeWhat it takes
$stringstringThe input string.
&$resultarrayA variable passed by reference, which will be set to an array containing the key-value pairs extracted from string. If the result parameter is not passed, a separate variable is set in the local scope for each key.

Return value

No value is returned.

Example

A runnable snippet:

$str = "first=value&arr[]=foo+bar&arr[]=baz"; // Recommended parse_str($str, $output); echo $output['first'], PHP_EOL; // value echo $output['arr'][0], PHP_EOL; // foo bar echo $output['arr'][1], PHP_EOL; // baz

Changelog

VersionChange
8.0.0result is no longer optional.
7.2.0Usage of parse_str() without a second parameter now emits an E_DEPRECATED notice.

See also

parse_url(), pathinfo(), http_build_query(), urldecode()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page parse_str() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.