Writings Tutorials

PHP Variables and Query Strings

Introduction

You have probably surfed the Internet enough to see the different ‘types’ of URLs you see on the address bar every time you visit websites. These URLs may be in the form of http://domain.tld/article or http://domain.tld/?p=1. The first type of URL is the static type. It is seen in most websites. The latter type, the dynamic type, has a query string, p=1. This type is usually used in blog-type websites (although there are options to transform URLs to the first type in blogs). In this tutorial, you will learn how to create and use query strings in URLs.

PHP Variables

In programming languages, variables are used to temporarily store values, like numbers and strings. In PHP, variables are declared using this format:

$variablename = value;

…where $variablename is the name of the variable, and value is the value of the variable.

If the value is a string, it must be enclosed in single quotes (' '), and if the value has a variable in it, it must be enclosed in double quotes (" ").

Always remember that PHP variables start with a dollar sign.

Query Strings

Query strings are ‘strings’ found in URLs that usually look like a variable declaration minus the dollar sign. Here is an example:

http://affeli.us/index.php?x=1

Above, x=1 is the query string. In this URL, it tells the server that $x (variable x) is equal to 1.

For this to work, index.php should have this code:

<?php
$eks = $_GET['x'];
?>

The $_GET gets the value of x, which—in the case of the URL above—is 1. As a result, the value of x, in turn, becomes the value of $eks. With the URL above, $eks is then equal to 1. Every time you change the value of x in the URL, the value of $eks also changes.

Two or more query strings can be placed in the URL, too. To connect them, you just have to use the ampersand symbol (&). E.g., https://affeli.us/what.php?y=yes&n=no and even https://affeli.us/now.php?cat=yeah&subcat=lieu&p=45

Applications of Both

Now that you’ve learned how variables and query strings work, take a look at this bunch of code:

<?php
$page_id = $_GET['p'];

include("$page_id.php");
?>

Given that index.php is the file where those codes are placed, I typed this URL on the address bar: https://affeli.us/index.php?p=67890 What will happen? If you thought that the PHP file with the filename 67890.php will be ‘included’ (and perhaps outputted), then your thought is correct.

Here is another example. Given that hello.php has these codes:

<?php
$greeting = $_GET['g'];

if( $greeting == 'hi' ) echo 'Good morning!';
else if( $greeting == 'hello' ) echo 'Good afternoon!';
else echo 'Goodbye~';
?>

And I typed https://affeli.us/hello.php?g=hello, what will be the output?

My last example concerns outputting images. If show.php has these codes:

<?php
$img = $_GET['img'];
$ext = $_GET['ext'];
?>
<html>
    <head>
    ...
    </head>
    <body>
        <img alt="" src="<?php echo $img.'.'.$ext ?>" />
    </body>
</html>

Then I inputted https://affeli.us/show.php?img=gaia&ext=png on the address bar, what will happen?

The image gaia.png will be displayed.

Be Off to Your Own

There are so many other applications you can use PHP variables and query strings for. They are extremely useful for instances where you need dynamic outputting.

Questions? Just drop a comment and I will answer them if I can! :)