PHP has global variables which can be accessed within any scope of your script. Three of these variables ($_GET
, $_POST
, $_COOKIE
) are stored within a fourth variable ($_REQUEST
).
$_GET
An associative array of variables passed to the current script via the URL parameters.
Consider the following example in which a URL is sent and accessed.
http://www.example.com/myPage.php?myVar=myVal
echo $_GET["myVar"]; // returns "myVal"
$_POST
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.
An example of this being used is as follows.
<form action="somePage.php" method="POST">
<input type="text" name="myVar" value="myVal" />
<input type="submit" name="submit" value="Submit" />
</form>
echo $_POST["myVar"]; // returns "myVal"
$_COOKIE
An associative array of variables passed to the current script via HTTP Cookies
setcookie("myVar", "myVal", time() + 3600);
echo $_COOKIE["myVar"]; // returns "myVal"
$_REQUEST
An associative array that by default contains the contents of
$_GET
,$_POST
and$_COOKIE
.
$_REQUEST
contains all three in one array and is accessed via $_REQUEST["myVar"]
.
Let's assume, for whatever reason, that I use the same name for my $_GET
, $_POST
, and $_COOKIE
.
What would the precedence be for what is stored in $_REQUEST
.
Assuming I set a sent data through the URL, posted through the form and, set a cookie with the same name as each other (a bizarre scenario, I know).
Lets say I used the name "example".
What would be the output of the following output?
if ($_REQUEST["example"] == $_GET["example"]) echo "GET";
else if ($_REQUEST["example"] == $_POST["example"]) echo "POST";
else if ($_REQUEST["example"] == $_COOKIE["example"]) echo "COOKIE";
If $_GET
, $_POST
, and $_COOKIE
all have a value stored with the same name; which one will $_REQUEST
store under said name?
PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form. The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag.
The PHP $_REQUEST is a PHP superglobal variable that is used to collect the data after submitting the HTML forms as the $_REQUEST variable is useful to read the data from the submitted HTML open forms. $_REQUEST is an associative array that by default contains contents of an $_GET, $_POST, and $_COOKIE.
The $_REQUEST variable is used to read the data from the submitted HTML form. Sample code: Here, the $_REQUEST variable is used to read the submitted form field with the name 'username'. If the form is submitted without any value, then it will print as “Name is empty”, otherwise it will print the submitted value.
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.
In php.ini
file there are 2 directives: request_order
and variables_order
request_order string
This directive describes the order in which PHP registers GET, POST and Cookie variables into the _REQUEST array. Registration is done from left to right, newer values override older values. If this directive is not set, variables_order is used for $_REQUEST contents. Note that the default distribution php.ini files does not contain the 'C' for cookies, due to security concerns.
and
variables_order string
Sets the order of the EGPCS (Environment, Get, Post, Cookie, and Server) variable parsing. For example, if variables_order is set to "SP" then PHP will create the superglobals $_SERVER and $_POST, but not create $_ENV, $_GET, and $_COOKIE. Setting to "" means no superglobals will be set. If the deprecated register_globals directive is on, then variables_order also configures the order the ENV, GET, POST, COOKIE and SERVER variables are populated in global scope. So for example if variables_order is set to "EGPCS", register_globals is enabled, and both $_GET['action'] and $_POST['action'] are set, then $action will contain the value of $_POST['action'] as P comes after G in our example directive value.
Taken from official documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With