parse_str

(PHP 4, PHP 5, PHP 7, PHP 8)

parse_strParses the string into variables

Description

parse_str(string $string, array &$result): void

Parses string as if it were the query string passed via a URL and sets variables in the current scope (or in the array if result is provided).

Parameters

string

The input string.

result

If the second parameter result is present, variables are stored in this variable as array elements instead.

Warning

Using this function without the result parameter is highly DISCOURAGED and DEPRECATED as of PHP 7.2.

Return Values

No value is returned.

Changelog

Version Description
8.0.0 result is no longer optional.
7.2.0 Usage of parse_str() without a second parameter now emits an E_DEPRECATED notice.

Examples

Example #1 Using parse_str()

<?php
$str 
"first=value&arr[]=foo+bar&arr[]=baz";

// Recommended
parse_str($str$output);
echo 
$output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz

// DISCOURAGED
parse_str($str);
echo 
$first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
?>

Because variables in PHP can't have dots and spaces in their names, those are converted to underscores. Same applies to naming of respective key names in case of using this function with result parameter.

Example #2 parse_str() name mangling

<?php
parse_str
("My Value=Something");
echo 
$My_Value// Something

parse_str("My Value=Something"$output);
echo 
$output['My_Value']; // Something
?>

Notes

Note:

All variables created (or values returned into array if second parameter is set) are already urldecode()d.

Note:

To get the current QUERY_STRING, you may use the variable $_SERVER['QUERY_STRING']. Also, you may want to read the section on variables from external sources.

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial