Feb 5 2009

Danish blogger Troels Knak-Nielsen wrote an interesting article about the PHP superglobals $_GET and $_POST today, bemoaning the fact they’re not technically named correctly. Reading through it, he makes a good point:

The current names are confusing and obscures the intention of HTTP; More descriptive names would have been $_QUERY instead of $_GET and $_FORM instead of $_POST

What’s he’s saying in a nutshell that if you send an HTTP POST request, such as:

POST /submit.php?param=HelloWorld HTTP/1.1
Host: asgrim.com
Content-Length: 29

name=James&url=www.asgrim.com

Then $_POST["name"] is “James”, $_POST["url"] is “www.asgrim.com”, but despite the form being submitted as POST, $_GET["param"] is HelloWorld. Technically, yes – he’s right. Logically, because name and url are part of the form data, the variables should be called $_FORM["name"], $_FORM["url"] and $_QUERY["param"].

He also makes the point that it might not be that big a disruption to change, sporting the introduction of superglobals instead of $_GET and $_POST as a replacement for register_globals as an example.

However, as one commenter, Rory, points out:

I can see what you’re getting at with $_QUERY and $_FORM but if you define your form as <form method="get"> your form data will be in $_QUERY not $_FORM which is possibly just as confusing for those who don’t understand the difference. I suspect the method attribute on forms is where $_GET and $_POST came from.

And my opinion? Well I can see what Troels is saying, and it’s a good valid point… I might have a little bit of cainophobia, but I’m happy with the way things are, and I’ll use Rory’s argument to back me up.

Leave a Reply