PHP parse_str()
PHP parse_str() function is used to parse the string into variables. The parse_str() function is a built-in function. The string which passes to parse_str() function gets converts to variables and their associated values. The parse_str() function accepts two parameters, where the first parameter is required and the second parameter is optional.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
parse_str(string,array);
Parameters:
- string: string parameter accept a parameter of string data types, the string is passed here in the format of string query through an URL. This parameter must pass.
- array: array parameter specifies the name of an array where to store the variables. So it specifies that all the variables are stored in an array. This parameter is an optional parameter. If an array name is not passed, then all the variables set by the function itself and overwrite by the same name of the variables.
Return value:
No return value.
Examples of PHP parse_str()
Given below are the examples of PHP parse_str():
Example #1
Example for parse_str() function to accept two-parameter. Here we use parse_str() function to accept a string of two variables that are to be stored into given array.
Code:
<?php parse_str("Ename=John&Eid=101&Esalary=30000&dept=sales", $Edetails); echo "Name : $Edetails[Ename] \n"; echo "Eid : $Edetails[Eid] \n"; echo "Salary : $Edetails[Esalary] \n"; echo "Department : $Edetails[dept] \n"; echo "$Edetails[Ename] having $Edetails[Eid] employee id, salary is $Edetails[Esalary] and working in $Edetails[dept] department." ; ?>
Output:
As in the above code, the parse_str() function accepted four variables by URL as “Ename=John&Eid=101&Esalary=30000&dept=sales” and $Edetailsis an array which stores all the variables as individual elements in it, from where farther all the variables are printing.
Example #2
Example for parse_str() function to accept one parameter.
Here we use parse_str() function to accept two-parameter of a string of four variables and we will see how an array internally stores all the variables.
Code:
<?php parse_str("Ename=John&Eid=101&Esalary=30000&dept=sales", $Edetails); echo " The Array Edetailscontain : \n "; print_r($Edetails); ?>
Output:
As in the above code, the parse_str() function accepted four variables by URL and as in output, it shows how an array stores all the variables.
Example #3
Here we use parse_str() function to accept only one parameter of a string of four variables which are to be store by their name itself.
Code:
<?php parse_str("Ename=John&Eid=101&Esalary=30000&dept=sales"); echo "Name : $Ename \n"; echo "Eid : $Eid \n"; echo "Salary : $Esalary \n"; echo "Department : $dept \n"; echo "$Ename having $Eid employee id, salary is $Esalary and working in $dept department." ; ?>
Output:
As in the above code, the parse_str() function accepted four variables by URL as “Ename=John&Eid=101&Esalary=30000&dept=sales” and no an array name is given so all the variables stored by their name itself which are farther printing directly.
Example #4
Here we use parse_str() function to accept a string of arrays elements to be store in a single array.
Code:
<?php parse_str( "a[]=John&a[]=101&a[]=30000&a[]=sales", $Edetails); // Display array echo " The Array Edetailscontain : \n "; print_r($Edetails); // Display each elements of an array echo "\n"; echo "Name : "; echo $Edetails[ 'a' ][0]; echo "\n"; echo "Eid : "; echo $Edetails[ 'a' ][1]; echo "\n"; echo "Salary : "; echo $Edetails[ 'a' ][2]; echo "\n"; echo "Department : "; echo $Edetails[ 'a' ][3]; ?>
Output:
As in the above code the parse_str() function accepted four variables of an array elements by URL as ” a[]=John&a[]=101&a[]=30000&a[]=sales” and all these elements are stored in an array Edetails and which are farther printing from the array.
Example #5
Here we use parse_str() function to accept a string of variables plus an array’s elements to be stored in a single array.
Code:
<?php // passing variable and array elements parse_str( "Ename=John&a[]=101&a[]=30000&a[]=sales", $Edetails); // Display array echo " The Array Edetailscontain : \n "; print_r($Edetails); // Display each elements of an array echo "\n"; echo "Name : "; echo $Edetails['Ename']; echo "\n"; echo "Eid : "; echo $Edetails[ 'a' ][0]; echo "\n"; echo "Salary : "; echo $Edetails[ 'a' ][1]; echo "\n"; echo "Department : "; echo $Edetails[ 'a' ][2]; ?>
Output:
As in the above code, the parse_str() function accepted one variable and three array elements by URL as “Ename=John&a[]=101&a[]=30000&a[]=sales ” and all these elements are store in an array Edetails and which are farther printing from the array.
Example #6
Here we see parse_str() function to pass and store a variable that may contain space.
Code:
<?php parse_str("E name=John&E id=101&E salary=30000&dept=sales"); // Display array elements echo "Name : $E_name \n"; echo "Eid : $E_id \n"; echo "Salary : $E_salary \n"; echo "Department : $dept \n"; ?>
Output:
As in the above code the parse_str() function accepted one variable where one of the variables contain space as “E name”. So to print this variable we should use ‘_’ in place of space as”$E_id”.
Conclusion
The PHP parse_str() function is a built in function which is used to parse the string into variables. We can pass variables, array elements and combination of variables and array in the query string format through the URL.
The above is the detailed content of PHP parse_str(). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
