PHP Tutorials

Query Strings

Why do I use those variable URLs? Learn all about them here...
1
One of the most frequent questions I get by email goes something along the lines of "Hey, how do you get those funky URLs?, what are they for? I want some!"

Well, actually, there is a good reason for them (although they do look pretty funky too right? =))

What they are
Query strings can be used to keep the number of files to a minimum when making pages or they can be used to store information from page to page. If you only want to make a site with one page you could use query strings and just do something like: index.php?page=home. Then every time you go to a new page you just change what "page" equals. When storing information from page to page you can have a page, lets say it generates a random number on load. Then you want to do something with that on another page, all you have to do is put a link to go to index.php?number=$randomnumber (this would be the variable that changes every time the page loads). Another example of a query string would be this tutorial, notice the url?
2
How they work
Query strings are very easy to code. Basically all it involves is an if statement and a variable declaration. If you wanted to have a two page site with one file (index.php), the code would look like this:

<html>
<head>
<title>Welcome!</title>
</head>
<body>
<?
$page = $_GET["page"];
if (($page == "home") or ($page == "")) {
echo ("Home page goes here.");
} else if($page == "about") {
echo ("About page goes here.");
}
?>
</body>
</html>

Its as easy as that. What that would do is when you load the page it would normally load as just index.php so the variable page wouldn't equal anything, so then it would so the home page. Then you click a link and the link would be index.php?page=about. Same for the home link instead page=home.
3
When it starts to get tricky is when the variable can differ. So if you wanted to do the random number thing and then you click a link and it does something with it you could do this.

Take the randomnumber code from Robouk's Random Number tutorial and the code would look like this.

<html>
<head>
<title>Welcome!<title>
</head>
<body>
<?
$number = $_GET["number"];
if ($number == "") {
echo ("Robouk's random number script goes here.");
echo ("Link to index.php?number=$randomnumber");
} else if($number != "") {
echo ("This is where you can do something with the randomnumber.");
}
?>
</body>
</html>

By setting a link as number=$randomnumber, the variable number would change every page load. So to see if the number equals something you can't give a certain number unless you only want to use that one number, so you see if number is NOT equal to nothing.

Query strings have never ending abilities and every language uses them, so they are important to learn.

End Tutorial.

Tutorial written by Booch.

Thanks in advance if you use this.

- Booch (www.provisionfx.com)
This tutorial was by Booch, brought to you by Robouk, please post any questions in the forum. Thank you.
BACK TO TUTORIALS