Home >Backend Development >PHP Tutorial >In-depth analysis of parse_str() function in PHP
The string stored in PHP
may be the value of multiple variables (such as URL
), so in order to finally get the value of each variable , how to parse a string into multiple variables, PHP
provides us with the parse_str()
function, which can successfully solve this problem. This article will take you to learn about it.
First, let’s take a look at the syntax of parse_str()
:
parse_str( string $string , array &$result )
$string The input string.
$result If the second variable $result is set, the variable will be stored in this array in the form of array elements. As an alternative, PHP 7.2 will abandon not setting parameters. Behavior.
Return value: No return value.
Code example:
1. The form with the second parameter:
<?php $str = "first=php&arr[]=foobar&arr[]=baz&info=.cn"; parse_str($str, $output); echo $output['first']; // value echo "<br>"; echo $output['arr'][0]; // foo bar echo "<br>"; echo $output['arr'][1]; // baz echo "<br>"; echo $output['info']; echo "<br>"; print_r($output);rrree
2. Only The form of a parameter:
输出: php foobar baz .cn Array ( [first] => php [arr] => Array ( [0] => foobar [1] => baz ) [info] => .cn )
<?php $str = "first=php&arr[]=foobar&arr[]=baz&info=.cn"; parse_str($str); echo $first."<br>"; echo $arr[0]."<br>"; echo $arr[1]."<br>"; echo $info[0];
Using parse_str() without the second parameter in PHP7.2.0
will generate a E_DEPRECATED
warning,#result
is a must in ##PHP8.0.0, so the second one is not recommended.
Recommended: 《2021 PHP interview questions summary (collection)》《php video tutorial》
The above is the detailed content of In-depth analysis of parse_str() function in PHP. For more information, please follow other related articles on the PHP Chinese website!