How to parse string into multiple variables in PHP? (code example)

藏色散人
Release: 2023-04-05 16:42:01
Original
3110 people have browsed it

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.

How to parse string into multiple variables in PHP? (code example)

Syntax:

parse_str($string, $array)
Copy after login

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; 
?>
Copy after login

Output:

Richik
20
Copy after login

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); 
  
?>
Copy after login

Output:

Array
(
    [roll_no] => 2
    [year] => 2nd
    [gpa] => 8.3
)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template