The parse_str()
function converts query string to variables.
It has the following format.
PHP parse_str() Function has the following syntax.
void parse_str ( string str [, array &arr] )
PHP parse_str() Function has the following syntax.
No value is returned.
For example, for URL like mypage.php?foo=bar&bar=baz
,
Query string is set to foo=bar&bar=baz
.
Variables parsed using parse_str() are converted to global variables.
<?PHP/*from w ww . ja va2s . c o m*/
if (isset($foo)) {
print "Foo is $foo<br />";
} else {
print "Foo is unset<br />";
}
parse_str("foo=bar&bar=baz");
if (isset($foo)) {
print "Foo is $foo<br />";
} else {
print "Foo is unset<br />";
}
?>
The code above generates the following result.
Optionally, we can pass an array as the second parameter to parse_str(), and it will put the variables into there.
<?PHP//from w w w . java 2s . c om
$array = array();
if (isset($array['foo'])) {
print "Foo is {$array['foo']}<br />";
} else {
print "Foo is unset<br />";
}
parse_str("foo=bar&bar=baz", $array);
if (isset($array['foo'])) {
print "Foo is {$array['foo']}<br />";
} else {
print "Foo is unset<br />";
}
?>
As we can see, the variable names are used as keys in the array, and their values are used as the array values.
The code above generates the following result.