PHP parse_str Function: Usage, Parameters, and Examples

This article explains PHP's parse_str() function, detailing its purpose of converting query strings into variables, describing its parameters and void return, and providing clear code examples demonstrating both basic usage and usage with an output array.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP parse_str Function: Usage, Parameters, and Examples

parse_str() parses a query string into variables.

If the input string is a URL query string, it creates variables in the current scope or stores them in an array when a second argument is provided.

Parameters

str : the input string.

arr (optional): an array that will receive the variables as elements.

Return value

None (void).

Example

<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

// Using second argument
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendString Parsingparse_strquery string
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.