parse_str()
Our article is about the PHP function parse_str(), which is used to parse a query string into variables. This function is useful for working with URLs and form
Our article is about the PHP function parse_str(), which is used to parse a query string into variables. This function is useful for working with URLs and form data in PHP. In this article, we will discuss the syntax and usage of parse_str(), as well as provide some examples.
The parse_str() function is used to parse a query string into variables. The syntax of the parse_str() function is as follows:
The PHP syntax of the parse_str()
void parse_str ( string $str , array &$arr [, string $separator = '&' ] )The function takes up to three parameters: $str, $arr, and $separator. The $str parameter is the query string to be parsed, and the $arr parameter is the array in which to store the parsed variables. The $separator parameter is an optional string used to separate variable assignments in the query string.
Note: If the second argument ($arr) is omitted, parse_str() will overwrite existing variables with the same names. This can pose a security risk if the input is untrusted.
Here is an example of how to use the parse_str() function:
Example of PHP parse_str()
<?php
$query = 'name=John+Doe&age=35&gender=male';
parse_str($query, $output);
print_r($output);
?>In this example, we have a string variable $query containing a query string. We use the parse_str() function to parse the query string into an array.
The output of this code will be:
Array
(
[name] => John Doe
[age] => 35
[gender] => male
)As you can see, the parse_str() function has parsed the query string into an array.
The parse_str() function is a useful tool for working with URLs and form data in PHP. It can help you parse a query string into variables, which is useful for various purposes such as data manipulation and validation. By mastering this function, you can become a more proficient PHP developer.
We hope this article has been helpful in understanding the parse_str() function in PHP.
Practice
What does the 'parse_str()' function in PHP do?