PHP parse_str() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, parse_str() turns a URL query string into an array.

It decodes values and understands array syntax like colors[]=red.

Syntax

Call parse_str() with the values you need. Start with this line.

PHP
parse_str("name=John&age=30", $data);
CallWhat it does
parse_str("a=1&b=2", $out)Sets $out to ["a" => "1", "b" => "2"].
parse_str("c[]=x&c[]=y", $out)Sets $out["c"] to ["x", "y"].
parse_str("city=New+York", $out)Decodes the plus sign into a space.

The second argument is required in PHP 8. The function returns nothing and writes the result into the variable you pass.

Library declarations
PHP
parse_str(string $string, array &$result): void

Parameters

ParameterDescription
stringRequired. The query string to parse, such as name=John&age=30.
resultRequired. A variable that receives the parsed array. It is passed by reference.

Return Value

Return ValueDescription
voidNothing is returned. The array is written to the second argument.

PHP Examples

Example 1 — Parse a Simple Query String

Pass a query string and a variable to receive the result.

PHP
<?php
parse_str("name=John&age=30", $data);

print_r($data);
echo $data["name"], PHP_EOL;

How It Works

  1. Write the query string. Pairs are joined with &.
  2. Pass a variable. $data is filled by reference.
  3. Print the array. Each key matches a name before =.
  4. Read a value. $data["name"] is "John".

Example 2 — Array Syntax in the Query

Square brackets in a name create nested arrays.

PHP
<?php
parse_str("colors[]=red&colors[]=blue", $data);

print_r($data);

How It Works

  1. Repeat the name with []. Each one adds an item.
  2. PHP builds an array. The key colors holds a list.
  3. Items are numbered. They start at index 0.
  4. Named keys work too. Use colors[a]=red for a string key.

Example 3 — Read Parameters from a URL

Combine parse_url() and parse_str() to get values out of a full link.

PHP
<?php
$url = "https://example.com/search?q=php+tips&page=2";

$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $params);

echo $params["q"], PHP_EOL;
echo $params["page"], PHP_EOL;

How It Works

  1. Start with a full URL. It has a query after the ?.
  2. Extract the query. parse_url with PHP_URL_QUERY returns that part.
  3. Parse it. The + sign becomes a space.
  4. Use the values. Both come back as strings.

Frequently Asked Questions

parse_str() parses a query string like "a=1&b=2" into an array, decoding URL-encoded values on the way.
void parse_str(string $string, array &$result). The result variable is required in PHP 8.
Older code called parse_str() with one argument and created variables. That was removed in PHP 8.0. Pass a second variable and read from it instead.
No. It returns nothing. The array is written to the variable you pass as the second argument.
Use http_build_query(). It is the opposite of parse_str().

Next: print

print outputs a string. It is a language construct, not a normal function.

print tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful