Skip to main content

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:

Prompt
Write a PHP script that displays an HTML form with a text input named "name" and a submit button, using the GET method. When the form is submitted, display "Hello" followed by the submitted name.
Output: PHP script

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:

Prompt
Write a PHP script that uses $_GET to collect a "subject" parameter from the URL query string and displays a message saying "Study" followed by the subject value, then also displays the value at the specified URL test.php?subject=PHP.
Output: PHP script

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).