GET String in PHP
Digg! This Page
This tutorial will show you how to use the GET string. If you do not know what a get string is, then hopefully you'll know by the end of this tutorial.About GET Strings
GET strings in PHP are basically the value within the address bar of a website. For example, if you went to a website and it had the address like this
http://www.dalehay.com/story.php?id=1Then the GET string will be called id and the value will be 1.
Step 1
To pull the value from the address bar all you need to do is firstly open your PHP tags.
<?php ?>Step 2
Now add a print command (to show the value) and then add the GET string after it
print $_GET['id'];This will show the value of 1 (if the PHP file address ends in ?id=1) Step 3
Using this simple GET string can be used to make a mini-dynamic website, for instance you could use this to have several pages running through one page.
<?php
echo "Welcome to my site"";
include("./$_GET[page].php");
echo "<hr>Come again soon.";
?>Then have links on your site goto index.php?page=news, create a page called news.php and it'll be shown if a visitor goes to that page.
Below is a very simple (full) example of using this.
index.php
<html>
<head>
<title>My Site</title>
</head>
<body>
<h2>My Site, Welcome!</h2>
<h4>Menu: <a href="./index.php">Home</a> - <a href="./index.php?page=news">News</a> - <a href="./index.php?page=contact">Contact</a></h4>
<?php
if($_GET[page]) {
include("./$_GET[page].php");
}
?>
<hr>Copyright, Blah Blah, 2008</body>
</html>
news.php
<b>Website Updated</b> Its a new year and I have a new website, if you like it give me some feedback.
contact.php
<b>Contact</b> You can contact me either by emailing me ( blah @ blah . com ) or phoning me ( 1234 567890 )Extra Addition
Using the above code, you'll notice that I have added an IF tag, this is because if no GET value is set then it tries finding an empty file that obviously cannot exist, so it returns an error, however sticking this IF tag around the include() statement will stop it from returning an error.
if($_GET[page]) {
}Basically just having the IF statement, in plain English terms saying "If the GET value page has any value, except blank, then proceed." Nice and easy!


