Home  >  Article  >  Backend Development  >  Examples to explain the definition and usage of php parse_str() function

Examples to explain the definition and usage of php parse_str() function

怪我咯
怪我咯Original
2017-05-27 10:45:521651browse

What is the function of php parse_str() function?

The parse_str() function in PHP parses the query string into variables. Its syntax is as follows:

Syntax

parse_str(string,array)

Parameter details:

Parameter Description
string Required. Specifies the string to be parsed.
array Optional. Specifies the name of the array in which the variable is stored. This parameter indicates that the variable will be stored in an array. If a second variable array is set, the variable will be stored in this array as an array element instead.

PS: If the array parameter is not set, the variables set by this function will overwrite the existing variables with the same name.

PS: The magic_quotes_gpc setting in the php.ini file affects the output of this function. If enabled, variables are converted by addslashes() before being parsed by parse_str() .

If string is the query string passed in by the URL, it is parsed into a variable and set to the current scope.

To get the current QUERY_STRING, you can use the $_SERVER['QUERY_STRING'] variable.

Example 1

Parse the query string into variables:

<?php
parse_str("name=Bill&age=60");
echo $name."<br>";
echo $age;
?>

Code running results:

Examples to explain the definition and usage of php parse_str() function

Example 2

Storing variables in the array:

<?php
parse_str("name=Bill&age=60",$myArray);
print_r($myArray);
?>

Code running results:

Examples to explain the definition and usage of php parse_str() function

[Recommended related articles]:

Detailed explanation of the definition and usage of php parse_url() function

The above is the detailed content of Examples to explain the definition and usage of php parse_str() function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn