What's the difference between $_POST, $_GET, and $_REQUEST?
I am bit confused about these super global variable ($_POST
, $_GET
, and $_REQUEST
) in PHP. In which scenario do I need to use these variables in PHP, and what are the main differences these three stand for?
$_POST is an associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request. You can use when you are sending large data to server or if you have sensitive information like passwords, credit card details etc
$_GET is an associative array of variables passed to the current script via the URL parameters. you can use when there is small amount of data, it is mostly used in pagination, page number is shown in the url and you can easily get the page number from URL using $_GET
$_REQUEST is a 'superglobal' or automatic global, variable. This simply means that it is available in all scopes throughout a script. It is an associative array that by default contains the contents of $_GET, $_POST and $_COOKIE (depending on request_order=
)
There are 2 methods to send HTML form data from 1 Page to another or HTML page to server side (In PHP).
POST
It is a method in which data gets sent using packet which is not visible to any user on web-browser. it is secured compared to GET method.
GET
It is a method in which data gets sent with URL which is visible to user in address-bar of any web-browser. So, it’s not secure as POST method.
Now, There are total three super global variables to catch this data in PHP.
-
$_POST
: It can catch the data which is sent using POST method. -
$_GET
: It can catch the data which is sent using GET method. -
$_REQUEST
: It can catch the data which is sent using both POST & GET methods.
Also with $_GET
superglobal variable can collect data sent in the URL from submit button.
Difference is:
$_GET retrieves variables from the querystring, or your URL.>
$_POST retrieves variables from a POST method, such as (generally) forms.
$_REQUEST is a merging of $_GET and $_POST where $_POST overrides $_GET.