What is $_GET in PHP: Simple Explanation and Usage
$_GET in PHP is a superglobal array that collects data sent via URL query parameters using the HTTP GET method. It allows you to access values from the URL easily, like form inputs or filters, by using the keys in the array.How It Works
Imagine you are sending a letter with some extra notes attached on the envelope. In web terms, the URL can carry extra information after a question mark (?), like notes for the website. This extra part is called the query string.
PHP's $_GET is like a mailbox that automatically collects these notes for you. When someone visits a URL like example.com?name=Anna&age=25, PHP fills the $_GET array with keys and values: name is "Anna" and age is "25".
This way, your PHP script can easily read what the visitor sent without extra work. It’s a simple way to get information from the URL, often used for searches, filters, or navigation.
Example
This example shows how to read values from the URL using $_GET and display them.
<?php // Check if 'name' and 'age' are set in the URL if (isset($_GET['name']) && isset($_GET['age'])) { $name = $_GET['name']; $age = $_GET['age']; echo "Hello, $name! You are $age years old."; } else { echo "Please provide your name and age in the URL."; } ?>
When to Use
Use $_GET when you want to get information from the URL, such as search terms, filters, or page numbers. It is perfect for data that is safe to show in the URL and does not change anything on the server.
For example, an online store might use $_GET to filter products by category or price. A blog might use it to show a specific page of posts.
Remember, $_GET is not for sensitive data like passwords because the information is visible in the URL.
Key Points
- $_GET is a PHP superglobal array for URL query parameters.
- It collects data sent via the HTTP GET method.
- Data is visible in the URL and easy to access by keys.
- Best for non-sensitive data like filters or search terms.
- Always check if keys exist before using them to avoid errors.
Key Takeaways
$_GET lets you access data sent in the URL query string easily.$_GET for safe, non-sensitive information like filters or search inputs.$_GET before using it to avoid errors.$_GET is visible in the URL and can be bookmarked or shared.$_GET for sensitive data like passwords or personal info.