PHP $_GET


Introduction

$_GET  is an associative array of variables passed to the current script via query string appended to URL of HTTP request. Note that the array is populated by all requests with a query string in addition to GET requests.

$HTTP_GET_VARS contains the same initial information, but has been deprecated

By default, client browser sends a request for URL on the server by HTTP GET method. A query string attached to URL may contain key=value pairs concatenated by & symbol. The $_GET associative array stores these key value pairs

Assuming that the URL in browser is http://localhost/testscript.php?name=xyz&age=20

Example

<?php
echo "Name : " . $_GET["name"] . "<br>";
echo "Age : " . $_GET["age"];
?>

Output

This will produce following result −

Name : xyz
Age : 20

In following example, htmlspecialchars() function is used to convert characters in HTML entities.

Character
Replacement
< (less than)&lt;
' (single quote)&#039; or &apos;
" (double quote)&quot;
& (ampersand)&amp;
 > (greater than) &gt;

Assuming that the URL in browser is http://localhost/testscript.php?name=xyz&age=20

Example

<?php
echo "Name: " . htmlspecialchars($_GET["name"]) . "<br>";
echo "age: " . htmlspecialchars($_GET["age"]) . "<br>";
?>

Output

This will produce following result −

Name : xyz
Age : 20

Updated on: 18-Sep-2020

382 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements