• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Get Query String from URL in php

Get Query String from URL in php

April 2, 2022 Leave a Comment

To get the query string from a URL in php, you can use $_GET super global to get specific key value pairs or $_SERVER super global to get the entire string.

// http://theprogrammingexpert.com/?key1=value1&key2=value2&key3=value3

echo $_GET['key1']; 
echo $_GET['key2'];

echo $_SERVER['QUERY_STRING'];

//Output:
value1
value2
key1=value1&key2=value2&key3=value3

A query string is a part of a URL that assigns values to specified parameters. When building web pages, query strings help us pass and access information to be used on our web pages.

When working in php, the ability to easily access and work with query strings is important.

To get a query string from a URL, there are a few different ways you can get the key value pairs.

To get a specific parameter from a query string, you can use $_GET super global to access individual parameters from the query string by name.

// http://theprogrammingexpert.com/?key1=value1&key2=value2&key3=value3

echo $_GET['key1']; 

//Output:
value1

If you want to get the entire query string, you can use the $_SERVER super global.

// http://theprogrammingexpert.com/?key1=value1&key2=value2&key3=value3

echo $_SERVER['QUERY_STRING'];

//Output:
key1=value1&key2=value2&key3=value3

After getting the entire query string, you can parse it for the key value pairs with the php parse_str() function.

Using $_GET to Get Query String Parameters in php

The $_GET super global variable allows us to get individual key value pairs from query strings.

There are a few additional cases which occur when using query strings that I want to share with you.

First, in a query string, you can have duplicate key names followed by square brackets. This will create a child array in $_GET for that key with numerically indexed values.

// http://theprogrammingexpert.com/?key[]=value1&key[]=value2&key[]=value3

echo $_GET['key'][0]; 
echo $_GET['key'][1]; 
echo $_GET['key'][2]; 

//Output:
value1
value2
value3

If the brackets are not empty, and have keys in them, then for a particular key, the child array belonging to the key will be an associative array.

// http://theprogrammingexpert.com/?key[ex1]=value1&key[ex2]=value2&key[ex3]=value3

echo $_GET['key']['ex1']; 
echo $_GET['key']['ex2']; 
echo $_GET['key']['ex3']; 

//Output:
value1
value2
value3

Using $_SERVER to Get Query String in php

You can also use the $_SERVER super global variable to get the entire query string. After getting the query string, you can parse it with the php parse_str() function.

// http://theprogrammingexpert.com/?key1=value1&key2=value2&key3=value3

query_string = $_SERVER['QUERY_STRING'];

parse_str(query_string);

echo $key1;
echo $key2;
echo $key3;

//Output:
value1
value2
value3

Hopefully this article has been useful for you to learn how to get query strings and work with them in php.

Other Articles You'll Also Like:

  • 1.  php array_flip() – Flip the Keys and Values of an Array in php
  • 2.  Using strtoupper() in php to Convert String to Uppercase
  • 3.  php sinh – Find Hyperbolic Sine of Number Using php sinh() Function
  • 4.  Check if String Starts with Given String in php with str_starts_with()
  • 5.  In PHP isset Method is Used For Checking if Variable is Defined
  • 6.  php array_filter() – Filter Array Elements with Function in php
  • 7.  php array_intersect() – Find Intersection of Two or More Arrays in php
  • 8.  php is_float() Function – Check if Variable is Float in php
  • 9.  php min – Find Minimum Value of Array in php
  • 10.  php atanh – Find Hyperbolic Arctangent of Number Using atanh() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy