What will happen if $_GET and $_POST has same variable when submitting form?

Lets consider following scenario when the form has an input field called username and the action URL also has the same name. See the code segment below:
<form action="?username=foo" method="post">
  <input type="text" name="username" value="bar"/><br />
  <input type="submit" value="submit me!" />
</form>


The above form submission will have a situation where it will have $_GET['username'] and $_POST['username']. Each of those variables have different values.

Now the question is what will be the value of $_REQUEST['username']?

Solution

PHP sets the order of superglobals in this order EGPCS (Environment, Get, Post, Cookie, and Server). As the Post comes after Get, the $_GET['username'] will be overwritten with $_POST['username'].

So the $_REQUEST['username'] will actually hold the value of $_POST['username'];

Comments