9

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
  • http://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get – Suraj Rao Mar 22 '17 at 04:06
  • 4
    Or the manual: http://php.net/manual/en/language.variables.superglobals.php - always a great resource ;-) – Qirel Mar 22 '17 at 04:06
  • It would be a good idea to never use `$_REQUEST` – zerkms Mar 22 '17 at 04:18
  • hi @suraj thanks for your reply, I tried that link, but it not included the request variable –  Mar 22 '17 at 04:28
  • that is regarding $GET and $POST in general with respect to http.. $_REQUEST as the answer will contain request values irrespective of type of request.. – Suraj Rao Mar 22 '17 at 04:31

4 Answers4

14

$_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=)

mario
  • 138,064
  • 18
  • 223
  • 277
Ch Hamza Javed
  • 260
  • 1
  • 9
  • `$_COOKIE` shouldn't be stroken over. Either delete it or specify for which versions of PHP it is valid for (or some other explanation, whatever is true). – Peter Mortensen Oct 29 '19 at 15:20
8

There are 2 methods to send HTML form data from 1 Page to another or HTML page to server side (In PHP).

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

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

  1. $_POST: It can catch the data which is sent using POST method.
  2. $_GET: It can catch the data which is sent using GET method.
  3. $_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.

Panagiotis
  • 133
  • 2
  • 8
5

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.
Mayank Pandeyz
  • 23,243
  • 3
  • 29
  • 52
  • 1
    Actually the `$_REQUEST` behaviour is determined by the `request_order` and `variables_order ` configuration directives. – zerkms Mar 22 '17 at 04:08
  • 1
    `$_REQUEST` Also includes values in the `$_COOKIE` super global ;) http://php.net/manual/en/reserved.variables.request.php – Jason Joslin Mar 22 '17 at 04:10
  • Finally I think you are answering my question. If I have a POST [name] and a GET[name] if I did a REQUEST [name] it would = POST [name] - correct (out with a `request_order`)? I am beginning to think REQUEST ain't that useful. When would u use it? – BeNice Dec 08 '18 at 17:44
  • Can you update your answer to qualify "where $_POST overrides $_GET" (as other comments have alluded to)? – Peter Mortensen Oct 29 '19 at 15:23
1

Well, to know better, please visit GET vs. POST:

1) Both $_GET and $_POST create an array e.g. array( key => value, key2 => value2, key3 => value3, ...). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.

2) Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

3) $_GET is an array of variables passed to the current script via the URL parameters.

4) $_POST is an array of variables passed to the current script via the HTTP POST method.

---- whereas $_REQUEST contains $_POST, $_GET and $_COOKIE .

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
M. K Hossain
  • 649
  • 1
  • 10
  • 24