PHP Superglobal - $_GET (With Examples)

By Shahnail Khan Last updated : December 14, 2023

PHP $_GET

$_GET is a PHP superglobal variable that retrieves data sent to the server through the URL. It's an associative array, storing data in key-value pairs, allowing for easy extraction of information from the URL.

Let's understand $_GET with a simple example before diving into the code.

Imagine you have a website with a search feature. Users can enter a search term in a box, and when they click "Search," the website shows results related to that term. Now, let's say a user wants to search for "technology."

If the website uses $_GET, the URL might look like this:

https://example.com/search.php?term=technology
  • The base URL is https://example.com/search.php.
  • ? signifies the start of the query parameters.
  • term is a key, and "technology" is the value assigned to that key.

Now, when the user clicks "Search", the website uses $_GET to extract the term from the URL. In this case, it retrieves the value "technology" associated with the key "term."

The advantage? Users can share the URL, and if someone clicks on it, they will directly see the search results for "technology" without needing to type anything.

Syntax of PHP $_GET

The syntax of using $_GET in PHP is:

$value = $_GET['parameter'];

Here,

  • $_GET: This is like a container in PHP that holds information sent through the URL.
  • 'parameter': Replace this with the name of the information you're looking for. It's like a label for the data you want.

Example of PHP $_GET

Let's use a simple example to understand $_GET in a real-world scenario.

Imagine you have a website with a page that displays information about different products. Each product has a unique identifier, and you want users to be able to view details about a specific product by clicking on a link.

Here's how you might use $_GET:

Product Page (product.php):

<!-- Link to view product details -->
<a href="product.php?id=123">View Product 123</a>

In this link, id is a parameter, and 123 is the value. The link points to the same page (product.php), but it includes information about the specific product (Product 123 in this case) in the URL.

PHP Script to Process the Product Details (product.php)

<?php
// Check if the 'id' parameter is set in the URL
if (isset($_GET["id"])) {
    // Retrieve the product ID from the URL
    $productId = $_GET["id"];

    // Fetch product details from a database or another data source
    // (In a real-world scenario, you might query a database)

    // Display product details
    echo "<h2>Product Details</h2>";
    echo "<p>Product ID: $productId</p>";
    // Display other product details...
} else {
    // Handle the case where 'id' parameter is not set
    echo "<p>Product ID is missing. Please select a product.</p>";
}
?>

In this PHP script, $_GET['id'] is used to retrieve the product ID from the URL. It then fetches and displays details about the specific product based on the provided ID.

Result:

If a user clicks on the link with id=123, the page will display details for Product 123.

If the link is clicked with a different ID, the script adapts to show details for that specific product.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.