PHP $_GET
$_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get".
$_GET can also collect data sent in the URL.
PHP $_GET in Form Handling
The page in the example below holds a form with one text input and a submit button. Clicking "Submit" sends whatever the user typed to the file named in the form's action attribute. Here that file is the page itself, so one script both displays the form and handles it. After the submit, the typed value can be read from $_GET:
Collect Form Data with $_GET
Read what the user typed into an input field with $_GET:
Output: PHP script
<!DOCTYPE html>
<html>
<body>
<form method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="name">
<input type="submit">
</form>
<?php
if (isset($_GET['name'])) {
echo "Hello " . $_GET['name'];
}
?>
</body>
</html>
PHP $_GET in Links
Since the GET method sends data through the URL, you can also use $_GET to collect data from links with query strings.
$_GET with a Link
A page that receives data from a link with a query string:
Output: PHP script
<!DOCTYPE html>
<html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at PromptCookbook";
?>
</body>
</html>
When to Use GET?
Data sent via GET is visible in the URL, so you should never use GET to send passwords or other sensitive information.
However, because the data is visible in the URL, it is possible to bookmark the page, which can be useful in some cases.
$_GET is best suited for:
- Search forms
- Page navigation
- Filtering results
- Any data that is not sensitive
Note: GET has a limit on the amount of data that can be sent (about 2048 characters).