Introduction
The $_GET superglobal variable in PHP is used to collect data sent using the HTTP GET method. When a user submits a form using the GET method or when data is included in the URL, PHP stores that data inside the $_GET array. Developers can then access and use this data in their scripts.
The GET method is simple and useful for sending small amounts of data, such as search queries or page numbers. However, since the data is visible in the browser URL, it should not be used for sensitive information like passwords.
Understanding how $_GET works is essential for PHP developers because it is widely used in many web applications and websites.
What is PHP $_GET?
In PHP, $_GET is a superglobal associative array that is used to collect form data sent using the GET method.
When data is sent through a URL or an HTML form using the GET method, PHP automatically stores that data inside the $_GET array. Each value in this array can be accessed using the name attribute of the form input field.
For example, if a user submits a form with a field named username, the value can be accessed in PHP using:
$_GET['username']
The GET method sends data through the URL in the form of a query string.
Example URL:
example.com/welcome.php?name=John&age=25
In this case:
- name = John
- age = 25
These values can be accessed in PHP like this:
echo $_GET['name']
echo $_GET['age']
Output:
25
Because the data is included in the URL, it is visible to users and can also be bookmarked or shared easily.
Why It Is Used
The $_GET method is used in many web applications because it is simple and convenient for sending small amounts of data.
Here are some common reasons why developers use $_GET:
1. Passing Data Through URL
Developers can send data directly through the URL using query parameters.
Example:
example.com/product.php?id=10
2. Search Functionality
Search forms often use the GET method so that search queries appear in the URL.
Example:
example.com/search.php?q=php+tutorial
3. Bookmarking Pages
Since GET data appears in the URL, users can bookmark pages with specific parameters.
4. Easy Debugging
Because the data is visible in the URL, developers can easily see what information is being sent.
5. Navigation and Filtering
GET is commonly used in pagination and filters such as:
products.php?page=2
Syntax
The syntax for using $_GET in PHP is very simple.
Accessing GET Data
$_GET['variable_name']
Example:
$name = $_GET['name'];
HTML Form Using GET Method
<form method="get" action="welcome.php">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
PHP Script
$name = $_GET['name'];
echo "Hello " . $name;
Explanation:
- The form sends data using the GET method.
- PHP receives the value using the $_GET array.
- The value is displayed on the page.
Example
Here is a simple example demonstrating how $_GET works.
HTML Form
<form method="get" action="result.php">
Enter your city:
<input type="text" name="city">
<input type="submit" value="Submit">
</for
PHP File (result.php)
$city = $_GET['city'];
echo "You live in " . $city;
Explanation:
- The user enters a city name in the form.
- When the submit button is clicked, the data is sent to result.php.
- The URL will look like this:
result.php?city=Delhi
- PHP retrieves the value using $_GET[‘city’].
- The script displays the message on the page.
This example shows how data can easily be passed through the URL.
Real-Life Example
A common real-life example of $_GET is a product page in an online store.
When a user clicks on a product, the product ID is often passed through the URL.
Example URL:
website.com/product.php?id=101
In this case, the value 101 represents the product ID.
PHP retrieves this value using:
$product_id = $_GET['id'];
The script then uses this ID to fetch product information from the database and display details such as:
- Product name
- Price
- Description
- Images
Another example is pagination on blogs or tutorials where the page number appears in the URL.
Example:
website.com/articles.php?page=2
Common Mistakes
Many beginners make some common mistakes when working with $_GET.
1. Using GET for Sensitive Data
GET sends data through the URL, so it should never be used for sensitive information like passwords.
2. Not Checking If the Value Exists
If a variable is not set, it may cause errors.
Example solution:
if(isset($_GET['name'])){
$name = $_GET['name'];
}
3. Not Validating User Input
Users can modify URL parameters manually, which may cause problems if the input is not validated.
4. Forgetting That URL Length Is Limited
GET requests have a limit on the amount of data that can be sent.
5. Trusting URL Data
Developers should always sanitize input before using it in queries or displaying it.
Conclusion
The $_GET superglobal variable in PHP is used to collect data sent through the URL using the GET method. It is simple, fast, and widely used for tasks such as search functionality, pagination, and passing parameters between pages.
However, because GET data is visible in the browser URL, it should not be used for sensitive information. Developers should always validate and sanitize user input to ensure security.