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:
Return value:
No return value.
Given below are the examples of PHP parse_str():
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 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.
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.
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.
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.
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”.
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!