This basic article will show you how to get values out of the URL with PHP.

Getting a variable out of the query string is one of the most basic operations in PHP. In fact, the query string variables are referred to as “superglobals“, which mean they are always available anywhere in your code.

If you haven’t used a server-side language before, or if you have only tried to get query string variables using JavaScript, this is much easier.

To access any variable in the URL, just type $_GET["itemname"] where itemname is the name of the item you want the value for.

Example Usage

The following example will print the word Google on the screen.

// Our example URL is http://solidlystated.com/?page=1&source=Google
 
echo $_GET["source"];

Watch Out For Errors

If the URL does not contain the GET variable you attempt to call, your script will throw an ugly error.

Therefore, always check for it’s existence first!

// Our example URL is http://solidlystated.com/?page=1
 
if( isset($_GET["source"]) ){ echo $_GET["source"]; }

That’s it! If you have more advanced questions about the $_GET superglobal or the query string, feel free to ask a question below.