Home  >  Article  >  Backend Development  >  In-depth analysis of parse_str() function in PHP

In-depth analysis of parse_str() function in PHP

autoload
autoloadOriginal
2021-04-20 11:24:142723browse

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[&#39;first&#39;];  // value
echo "<br>";
echo $output[&#39;arr&#39;][0]; // foo bar
echo "<br>";
echo $output[&#39;arr&#39;][1]; // baz
echo "<br>";
echo $output[&#39;info&#39;];
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!

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