0
0
PHPprogramming~5 mins

$_GET for URL parameters in PHP

Choose your learning style9 modes available
Introduction

$_GET lets you get information from the website address (URL). It helps your program know what the user wants by reading the details after the question mark in the URL.

When you want to know what a user typed in the URL to show specific content.
When you create a search page and want to get the search words from the URL.
When you want to pass small pieces of information between pages without using forms.
When you want to remember choices a user made by putting them in the URL.
When you want to create links that change what the page shows based on the URL.
Syntax
PHP
<?php
$value = $_GET['key'];
?>

$_GET is a special PHP array that holds all the URL parameters.

Each parameter is accessed by its name inside square brackets as a string.

Examples
This gets the 'name' from the URL and says hello to that name.
PHP
<?php
$name = $_GET['name'];
echo "Hello, $name!";
?>
This gets the 'page' parameter or uses 'home' if none is given.
PHP
<?php
$page = $_GET['page'] ?? 'home';
echo "You are on the $page page.";
?>
This checks the 'age' from the URL and tells if the user is an adult or minor.
PHP
<?php
$age = $_GET['age'];
if ($age >= 18) {
  echo "You are an adult.";
} else {
  echo "You are a minor.";
}
?>
Sample Program

This program reads 'color' and 'size' from the URL and shows them. If they are missing, it says 'unknown'.

PHP
<?php
// Example URL: example.com?color=blue&size=large
$color = $_GET['color'] ?? 'unknown';
$size = $_GET['size'] ?? 'unknown';
echo "Color: $color\n";
echo "Size: $size\n";
?>
OutputSuccess
Important Notes

Always check if a $_GET parameter exists before using it to avoid errors.

URL parameters are visible to users, so do not put sensitive information there.

Use htmlspecialchars() to safely show $_GET values on the page to avoid security issues.

Summary

$_GET reads information from the URL after the question mark.

It helps your PHP code know what the user wants or chose.

Always check if parameters exist and be careful with security.