PHP Basics

What is the purpose of $_GET in PHP Scripting?

$_GET variable is used in php to collect data that is passed through url. The general case where we use get in a php is during form submission. Form submission sends various data as parameter in the url.

At the recipient php file, the data present in the variable is collected.

How to use Get in PHP?

$_GET variable is super global variable and contains all the passed data as parameter in a url.

Example:

Consider Login Form

After user submits the form, the users provided email and password is sent to a php file lets say login-verification.php file. This file checks the provided information with the record available in the database. If provided login credentials matches to the available record, then user is granted permission to access the website other wise an error message is shown to the user.

Here we need to understand, before comparing with the available records in the database, the php file must collect the provided login credentials. $_GET super global variable contains the data that is sent through post method.

How can we know what are the values present in the $_GET variable?

By using var_dump or print_r, we can look into the global variable “$_GET” and know all the present parameters inside the variable.

How can we check $_GET contains any value?

By using isset function, we can check $_GET is empty or contains value.

example

<?php

if(isset($_GET)){

var_dump($_GET);

}

?>

To collect data from $_GET variable, we must have to know the keys.

Example

Collect email from $_GET

<?php

echo $_GET[“email”];

?>

Output: example@example.com