Current Location: Home> Latest Articles> In-depth Guide to PHP's parse_str Function: Usage and Examples

In-depth Guide to PHP's parse_str Function: Usage and Examples

M66 2025-06-30

Using parse_str Function in PHP

In this article, we will explore the parse_str function in PHP, which is used to convert the query string in a GET request into variables that can be used within PHP.

In everyday PHP development, handling query strings is a common task, typically done via GET requests, where data is passed into the URL of a script. PHP provides a superglobal variable $_GET, which stores all the query parameters. However, there are times when you need to directly process the raw query string and convert it into PHP variables. The parse_str function provides an easy solution for this purpose.

This article will guide you through the syntax and usage of the parse_str function, with several practical examples to demonstrate its capabilities.

Syntax of the parse_str Function

The basic syntax of the parse_str function is as follows:

<span class="fun">parse_str(string $string, array &$result): void</span>

The parse_str function has two parameters: the first one is the query string you want to parse, and the second is an array that will be populated with the parsed variables.

Note that parse_str does not return any value. Instead, you must use the second parameter to get the results. Starting from PHP 7.2, the second parameter is mandatory. If you omit it, parse_str will throw an error.

Additionally, the parse_str function automatically decodes URL-encoded values, so you don't need to apply the urldecode function yourself.

Basic Example

Here's a simple example of how to use the parse_str function:

<?php
$string = 'first_name=John&last_name=Richards&age=30';
parse_str($string, $result);
print_r($result);

In this example, the query string is parsed into a PHP array, where the variable names become array keys, and the values from the query string are assigned to those keys. The output would be:

Array
(
    [first_name] => John
    [last_name] => Richards
    [age] => 30
)

Handling Array Variables

If the query string contains array variables, the parse_str function can handle them properly. For example:

<?php
$string = 'foo=1&bar[]=1&bar[]=2';
parse_str($string, $result);
print_r($result);

After parsing, the bar variable will be converted into an array, as shown below:

Array
(
    [foo] => 1
    [bar] => Array
    (
        [0] => 1
        [1] => 2
    )
)

If you don't use the [] syntax for array variables, parse_str will treat it as a regular variable and only keep the last value. This behavior is slightly different from most query string parsers.

Handling Special Characters

Sometimes, query strings include variable names with spaces or dots. Since PHP does not allow spaces or dots in variable names, parse_str will automatically replace these characters with underscores. Here's an example:

<?php
$string = 'my name=John&my.email=john@example.com';
parse_str($string, $result);
print_r($result);

The output will be:

Array
(
    [my_name] => John
    [my_email] => john@example.com
)

URL Encoded Query Strings

The parse_str function automatically decodes URL-encoded values, so you don't need to manually apply urldecode. Here's an example with URL-encoded values:

<?php
$string = 'name=John%20Richards&profile_url=http%3A%2F%2Fexample.com%2Fmyprofile';
parse_str($string, $result);
print_r($result);

The output would be:

Array
(
    [name] => John Richards
    [profile_url] => http://example.com/myprofile
)

Conclusion

In this article, we've explored the parse_str function in PHP, which is incredibly useful for converting query strings into variables. I hope the examples provided help you better understand how to use this function in your own PHP projects, improving your development workflow.