How to get parameters from a URL string in PHP?
Last Updated :
11 Jul, 2025
The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions.
Note: The page URL and the parameters are separated by the ? character.
parse_url() Function
The parse_url() function is used to return the components of a URL by parsing it. It parses a URL and returns an associative array that contains its various components.
Syntax:
parse_url( $url, $component = -1 )
parse_str() Function
The parse_str() function is used to parse a query string into variables. The string passed to this function for parsing is in the format of a query string passed via a URL.
Syntax:
parse_str( $string, $array )
Approach
Parse the URL string using parse_url() function which will return an associative array that contains its (passed URL) various components. The query of the array returned by parse_url() function which contains a query string of URL.
Example 1: Below examples uses parse_url() and parse_str() function to get the parameters from URL string.
PHP
<?php
// Initialize URL to the variable
$url = 'https://www.geeksforgeeks.org/?name=Tonny';
// Use parse_url() function to parse the URL
// and return an associative array which
// contains its various components
$url_components = parse_url($url);
// Use parse_str() function to parse the
// string passed via URL
parse_str($url_components['query'], $params);
// Display result
echo ' Hi '.$params['name'];
?>
Example 2: Below example uses parse_url() and parse_str() function to get the parameters from URL string.
PHP
<?php
// Initialize URL to the variable
$url = 'https://www.geeksforgeeks.org/register/';
// Use parse_url() function to parse the URL
// and return an associative array which
// contains its various components
$url_components = parse_url($url);
// Use parse_str() function to parse the
// string passed via URL
parse_str($url_components['query'], $params);
// Display result
echo ' Hi '.$params['name'].' your emailID is '.$params['email'];
?>
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Explore
Basics
Array
OOPs & Interfaces
MySQL Database
PHP Advance