Updated April 3, 2023
Introduction to 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.
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.
Recommended Articles
This is a guide to PHP parse_str(). Here we discuss the introduction, syntax, and examples of PHP parse_str() along with code implementation. You may also have a look at the following articles to learn more –