Support PHP Version: PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1, PHP 8.2, PHP 8.3 With Latest All Version Support.
Hello Friends Today, through this tutorial, I will tell you How to Use `parse_str()` function using PHP, PHP 8, PHP 8.1, PHP 8.2 With Example. The `parse_str()` function in PHP is used to parse query strings into variables. It takes a query string as input and parses it into variables in the current scope. In PHP 8.2, the behavior of `parse_str()` remains the same as in previous versions of PHP. Here’s how you can use it with an example:
<?php // Example query string $queryString = "name=John&age=30&city=New+York"; // Parse the query string into variables parse_str($queryString, $parsedArray); // Access the parsed variables echo "Name: " . $parsedArray['name'] . "<br>"; echo "Age: " . $parsedArray['age'] . "<br>"; echo "City: " . $parsedArray['city'] . "<br>"; ?>
Output:
Name: John Age: 30 City: New York
In this example:
1. We have a query string `$queryString` containing key-value pairs separated by `&`.
2. We use the `parse_str()` function to parse this query string into variables. The parsed variables are stored in the array `$parsedArray`.
3. We then access individual variables from `$parsedArray` using their respective keys.