PHP | parse_str() Function
The parse_str() function is a built-in function in PHP which parses a query string into variables. The string passed to this function for parsing is in the format of a query string passed via a URL.
Syntax :
parse_str($string, $array)
Parameters: This function accepts two parameters as shown in the above syntax out of which the first parameter must be supplied and the second one is optional. All of these parameters are described below:
- $string: It specifies the string to parse.
- $array: This is an optional parameter which specifies the name of an array to store the variables. This parameter indicates that the variables will be stored in an array.
Examples:
Input : "name=Richik&age=20"
Output : $name = Richik
$age = 20
Input : "roll_no=2&year=2nd&gpa=8.3"
Output : $roll_no = 2
$year = 2nd
$gpa = 8.3
Below programs illustrate the parse_str() function in PHP:
Program 1 :
<?php parse_str("name=Richik&age;=20"); echo $name."\n".$age; ?> |
Output:
Richik 20
Program 2:In this program we will store the variables in an array and then display the array using print_r() function.
<?php parse_str("roll_no=2&year;=2nd&gpa;=8.3", $array); print_r($array); ?> |
Output:
Array
(
[roll_no] => 2
[year] => 2nd
[gpa] => 8.3
)
Reference:
http://php.net/manual/en/function.parse-str.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- p5.js | box() Function
- p5.js | sq() function
- p5.js | nfp() Function
- p5.js | abs() function
- CSS | var() Function
- p5.js | nf() Function
- D3.js | d3.map.set() Function
- PHP Ds\Set get() Function
- D3.js | d3.min() function
- PHP | each() Function
- PHP | pi( ) Function
- CSS | url() Function
- PHP | pow( ) Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



