URL Variables in PHP: What are they, and how are they used?
13 Oct 2007 Quan Quach 7 comments 5,468 views
Have you ever wondered how to pass variables or arguments to a PHP script? The simple answer lies within URL variables. URL variables are variables appended onto the end of a URL address, allowing them to be used within PHP scripts. Basically, URL variables can be considered external variables that are brought inside the PHP script. This tutorial will provide a very basic example of how URL variables are used within PHP scripts.
The following php code is used to show how URL variables work:
1 2 3 4 5 6 | <?php $a = $_GET['a']; //stores the URL variable into a variable that php can use $b = $_GET['b']; echo("The URL variable you inputted was {$a} and {$b}"); ?> |
- You can access this php file here. Alternatively, you can save this code as test.php and upload it to your own webhost.
- When you navigate to the address where the php script is located, you should see the following text in your browser:
The URL variable you inputted was and
Since you haven’t inputted any URL variables yet, the output doesn’t say anything.
- Now, copy and paste
?a=hello&b=worldto the end of the URL of the php script that you just opened. By doing this, you are specifying the variables that will be sent to the PHP script.The following should now appear in your browser:
http://www.blinkdagger.com/tutorials/php/URLvariables/test.php?a=hello&b=world
- When you navigate to the page again, you should see the following:
The URL variable you inputted was hello and world
- As can be seen, this simple script takes URL variables and uses it within the PHP script. The string “hello” was assigned to $a, and the string “world” was assigned to $b within the php script. At the end of the script, it is echoed. You can try entering in other variables as well. Using URL variables greatly increases the flexibility of your php script by making them dynamic and adaptable.
7 Responses to “URL Variables in PHP: What are they, and how are they used?”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>


But what you’ve done can make a big site vulnerability because he can do this ?a=alert(”….”); and lots of bad scripts
to get rid of this you cand ad this
$a = addslashes($_GET['a']);
or
$a = htmlspecialchars($_GET['a']);
It’s always good to get back to the very basics from time-to-time, I find! Well explained - very well laid out and all. Kudos.
I believe this small tutorial is for people new with php, so I don’t see any problem like cigraphics sayd. It’s just to give another point of view, and this kind of idea help people understand things better.
good article!
regards!
i need help in php
Text to be displayed
Here i m not getting $num value when i print echo $_GET['id'];
tocomeinhandy
thank you !!! an old script of mine stopped working, and thanks to this straightforward post i’ve got things going again. thanks a bunch!
shawaor jinis banaiso………..