PHP 8 reference · String Functions

PHP parse_str() Function

Parse a query string such as a=1&b=2 into an array, the reverse of http_build_query(). The result argument is required.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
string The input string.
result The array that receives the variables.

Return value

Nothing; the values go into result.

Examples

Parse a query stringTry it yourself
<?php
parse_str("page=2&sort=name&tags[]=php&tags[]=web", $params);
print_r($params);

Output

Array
(
    [page] => 2
    [sort] => name
    [tags] => Array
        (
            [0] => php
            [1] => web
        )

)
Query from a full URLTry it yourself
<?php
$url = "https://example.com/search?q=tea+%26+cake&page=3";
parse_str(parse_url($url, PHP_URL_QUERY), $query);
echo $query["q"], " / ", $query["page"], "\n";

Output

tea & cake / 3

Version history

VersionChange
PHP 4.0Added
PHP 8.0The result argument is required; without it, variables were created in the current scope.

Was this page helpful?