Home >Backend Development >PHP Tutorial >How to parse string into multiple variables in PHP? (code example)
The parse_str() function is a built-in function in PHP. Its function is to parse the query string into variables. The string passed to this function for parsing is in the format of the query string passed through the URL.
Syntax:
parse_str($string, $array)
Parameters:
This function accepts two parameters as shown in the above syntax, of which the first one must be provided parameters, the second parameter is optional.
The parameters are described as follows:
$string: It specifies the string to be parsed.
$array: This is an optional parameter that specifies the name of the array in which the variable is stored. This parameter indicates that the variable will be stored in an array.
Below we will introduce to you with a simple example, PHP's method of parsing strings into variables, that is, the use of the parse_str() function:
Code example 1:
<?php parse_str("name=Richik&age=20"); echo $name."\n".$age; ?>
Output:
Richik 20
Code Example 2:
Here we store the variables in an array and then display the array using the 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 )
Related recommendations: "PHP Tutorial"
This article is about PHP parsing strings into multiple An introduction to the method of variables, I hope it will be helpful to friends in need!
The above is the detailed content of How to parse string into multiple variables in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!