Home >>PHP String Functions >PHP parse_str() Function
PHP parse_str() function is used to parses a query string into variables. We can say that the string which we pass to this function get conveted into variables & their respected values. The string which is passed to this function for the parsing is in the format of a query string passed through a URL. This function accepts two parameters, out of which the first parameter is required and the second one is optional.
Syntax:
parse_str($string, $array);
Parameter | Description |
---|---|
string | This is a required parameter. This parameter contains the string to be parsed. |
array | This is an optional parameter(required in PHP 7.2 & above). This parameter contains the name of an array to in which the variables will be stored. |
Here is an example of parse_str() function in PHP:
<html> <body> <?php parse_str("Name=Jerry&Company=PHPTPOINT"); echo $Name."<br>"; echo $Company; ?> </body> </html>
Here is an another example of parse_str() function in PHP:
<html> <body> <?php parse_str("Name=Jerry&Company=PHPTPOINT&Age=21&Place=Noida"); echo $Name."<br>"; echo $Company."<br>"; echo $Age."<br>"; echo $Place."<br><br>"; echo "$Name is $Age years old. He is from $Place & works for $Company."; ?> </body> </html>
Here is another example of parse_str() function in PHP using the $array parameter:
<html> <body> <?php parse_str("name=PHPTPOINT&place=Noida",$Array); print_r($Array); ?> </body> </html>