Lesson 10: Passing variables in a URL
Ak Patel
---
How does it work?
Maybe you have wondered why some URLs look something like this:http://html.net/page.php?id=1234
The answer is that the characters after the question mark are an HTTP query string. An HTTP query string can contain both variables and their values. In the example above, the HTTP query string contains a variable named "id", with the value "1254".
Here is another example:
http://html.net/page.php?name=jkl
How to get the variable with PHP?
Let's say you have a PHP page named people.php. Now you can call this page using the following URL:people.php?name=jkl
$_GET["name"]
<html> <head> <title>Query string</title> </head> <body> <?php // The value of the variable name is found echo "<h1>Hello " . $_GET["name"] . "</h1>"; ?> </body> </html>
Several variables in the same URL
You are not limited to pass only one variable in a URL. By separating the variables with &, multiple variables can be passed:people.php?name=jkl&age=24
$_GET["name"] $_GET["age"]
<html> <head> <title>Query string </title> </head> <body> <?php // The value of the variable name is found echo "<h1>Hello " . $_GET["name"] . "</h1>"; // The value of the variable age is found echo "<h1>You are " . $_GET["age"] . " years old </h1>"; ?> </body> </html>