EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP parse_str()

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP parse_str()

PHP parse_str()

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

php parse_str() 1

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:

php parse_str() 2

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.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

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:

one parameter of string

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:

php parse_str() 4

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:

arrays elements

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:

pass and store a variables which may contain space

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 of PHP parse_str() along with examples. You may also have a look at the following articles to learn more –

  1. PHP Pagination
  2. PHP header()
  3. PHP wordwrap()
  4. PHP levenshtein()

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More