What is $_ get in php?

PHP Superglobal - $_GET


Super global variables are built-in variables that are always available in all scopes.


PHP $_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.

Assume we have an HTML page that contains a hyperlink with parameters:


Test $GET


When a user clicks on the link "Test $GET", the parameters "subject" and "web" are sent to "test_get.php", and you can then access their values in "test_get.php" with $_GET.

The example below shows the code in "test_get.php":

Example


echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>


Try it Yourself »

Tip: You will learn more about $_GET in the PHP Forms chapter.



(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

$_GETHTTP GET variables

Description

An associative array of variables passed to the current script via the URL parameters (aka. query string). Note that the array is not only populated for GET requests, but rather for all requests with a query string.

Examples

Example #1 $_GET example

echo 'Hello ' htmlspecialchars($_GET["name"]) . '!';
?>

Assuming the user entered http://example.com/?name=Hannes

The above example will output something similar to:

Notes

Note:

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

Note:

The GET variables are passed through urldecode().

CleverUser123

8 months ago

If you're tired of typing $var = $_GET['var'] to get variables, don't forget that you can also use:

extract($_GET, EXTR_PREFIX_ALL, "g")

So if you have $_GET['under'], you can do $g_under. It's way shorter if you have more get elements! If you don't want prefix, do

extract($_GET)

to get normal. So $_GET['under'] would become $under. Might not extract under if it already exists, however.

An Anonymous User

1 year ago

// It is important to sanitize
// input! Otherwise, a bad actor
// could enter ''
// in a URL parameter. Assuming you echo it, this
// would inject scripts in an XSS attack.
//
// The solution:
$NAME = $_GET['NAME'];
// Bad:
echo $NAME;
// that one is vulnerable to XSS
// Good:
echo htmlspecialchars($NAME);
// Sanitizes input thoroughly.
?>

PHP $_GET

What is $_ get in php?
What is $_ get in php?

The $_GET variable is used to collect values from a form with method="get".


The $_GET Variable

The $_GET variable is an array of variable names and values sent by the HTTP GET method.

The $_GET variable is used to collect values from a form with method="get". Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and it has limits on the amount of information to send (max. 100 characters).

Example

Name: Age:

When the user clicks the "Submit" button, the URL sent could look something like this:

http://www.w3schools.com/welcome.php?name=Peter&age=37

The "welcome.php" file can now use the $_GET variable to catch the form data (notice that the names of the form fields will automatically be the ID keys in the $_GET array):

Welcome .
You are years old!



Why use $_GET?

Note: When using the $_GET variable all variable names and values are displayed in the URL. So this method should not be used when sending passwords or other sensitive information! However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

Note: The HTTP GET method is not suitable on large variable values; the value cannot exceed 100 characters.


The $_REQUEST Variable

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.

The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

Example

Welcome .
You are years old!



What is $_ get in php?
What is $_ get in php?





What is $_ get in php?
  

Get Your Diploma!

W3Schools' Online Certification Program is the perfect solution for busy professionals who need to balance work, family, and career building.

The HTML Certificate is for developers who want to document their knowledge of HTML, XHTML, and CSS.

The JavaScript Certificate is for developers who want to document their knowledge of JavaScript and the HTML DOM.

The XML Certificate is for developers who want to document their knowledge of XML, XML DOM and XSLT.

The ASP Certificate is for developers who want to document their knowledge of ASP, SQL, and ADO.

The PHP Certificate is for developers who want to document their knowledge of PHP and SQL (MySQL).


What is $_ GET and $_ POST PHP?

$_GET, and $_POST are array variables of PHP which are used to read submitted data by HTML form using the get and post method accordingly.

What is the difference between $_ POST and $_ GET?

Difference is: $_GET retrieves variables from the querystring, or your URL.> $_POST retrieves variables from a POST method, such as (generally) forms.

Why do we use GET method in PHP?

Some of the advantages of using the GET Method are: Since the FORM data sent by the GET Method is appended in the URL, the webpage can be bookmarked with specific query string values. Any request made using GET Method remains in the browser history. GET Method requests can be cached.

Is $_ GET an array?

$_GET is a built-in variable of PHP which is an array that holds the variable that we get from the URL.