PHP parse_str()

WBOY
Release: 2024-08-29 12:54:20
Original
564 people have browsed it

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);
Copy after login

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." ;
?>
Copy after login

Output:

PHP parse_str()

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);
?>
Copy after login

Output:

PHP parse_str()

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." ;
?>
Copy after login

Output:

PHP parse_str()

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];
?>
Copy after login

Output:

PHP parse_str()

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];
?>
Copy after login

Output:

PHP parse_str()

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";
?>
Copy after login

Output:

PHP parse_str()

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!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template