-1

If found this question $_POST vs. $_SERVER['REQUEST_METHOD'] == 'POST' and people seems to prefer check $_SERVER['REQUEST_METHOD'] == 'POST' rather than check $_POST or isset($_POST).

(Already, i don't understand why people check isset($_POST), while it is always set)

But what if we check !empty($_POST) instead all of this ?

I mean, the $_POST superglobale is always defined on a page as an empty array, so just checking if it's not empty mean thats a form with the post method has been submitted, am I wrong ? If it's not empty, we can check each fields we want inside.

I don't get why we need to add an additional step by checking

$_SERVER['REQUEST_METHOD'] == 'POST'

(Also, triple "=" wouldn't be better ?)

I've searched but found nothing about it. And if i find nothing, it probably means that i'm saying nonsense ^^.

Can someone tell me if I am wrong ?

Alexis Vandepitte
  • 2,077
  • 2
  • 12
  • 28
  • 2
    *"`$_POST` superglobale is always defined"* – So why are you using `empty` then? Just `if ($_POST)`! – deceze Feb 02 '18 at 15:02
  • Because I don't want to check if it is defined ... i want to check if a form with values has been submited. So it is not empty it means that a form has been submitted. – Alexis Vandepitte Feb 02 '18 at 15:03
  • Yeah, right, so… `if ($_POST)`. – [The Definitive Guide To PHP's isset And empty](http://kunststube.net/isset/) – deceze Feb 02 '18 at 15:04
  • that's not checking if it's defined, it's checking if it contains a *truthy* value – billyonecan Feb 02 '18 at 15:04
  • So, having established that `if (!empty($_POST))` is the same as `if ($_POST)` (modulus error reporting)… is there any question left to answer here that hasn't already been answered in that other question you link to? – deceze Feb 02 '18 at 15:06
  • I understand but most case you want to check it you have something in it ... so why first check if it is method post and after check if it is not empty while just checking if its not empty is enough – Alexis Vandepitte Feb 02 '18 at 15:07
  • Because, some inputs may already contain value; that's one of the reasons. – Funk Forty Niner Feb 02 '18 at 15:08
  • `if(!empty($_POST))` is not the same as `if($_POST)` ... ?? Or i'm crazy. But, one check if it is not empty, and the other check if it is not false or null – Alexis Vandepitte Feb 02 '18 at 15:10
  • That Q&A they reference, I feel it's been answered/covered in an answer given in there https://stackoverflow.com/a/409376/1415724 @deceze would you agree? – Funk Forty Niner Feb 02 '18 at 15:10
  • Read my aforelinked article on `isset` and `empty`… – deceze Feb 02 '18 at 15:10
  • The terrifically confusing part here really is PHP's naming of `$_POST` and `$_GET`. In other languages/frameworks, those are appropriately named something along the lines of body data/body arguments and query parameters, which is what they really are. `$_POST` does not really have all that much to do with HTTP `POST` requests, and neither does `$_GET` have anything to do with HTTP `GET`. – deceze Feb 02 '18 at 15:12
  • *"(Also, triple "=" wouldn't be better ?)"* - See the differences here in [The 3 different equals](https://stackoverflow.com/questions/2063480/the-3-different-equals) – Funk Forty Niner Feb 02 '18 at 15:12
  • Thank you everybody, i've read everything you sent me. It helped a lot – Alexis Vandepitte Feb 02 '18 at 15:35

1 Answers1

3

PHP's choice to name $_POST and $_GET as they did was terrible, and is mostly leading to confusion here. $_GET contains the request URL's query parameters, while $_POST contains the parsed request body data (if it's application/x-www-form-urlencoded).

Any URL, and thereby any request, can contain URL query parameters, so $_GET can be and is used with any sort of request, not just HTTP GET requests. HTTP POST and PUT requests (and arguably any other verb you make up) can contain a urlencoded request body, which would/could end up in $_POST (not sure off the top of my head whether PHP bothers parsing body data for anything but POST requests). A POST or PUT request also doesn't have to contain body data; if an empty POST request makes sense in your application, so be it.

So, $_POST, POST, $_GET, GET, request body data, query parameters etc. are all rather orthogonal to each other.

If you want to check the method of the HTTP request, check $_SERVER['REQUEST_METHOD']. If you want to know whether any urlencoded body data was sent, check whether there's data in $_POST. For that purpose, if ($_POST) is enough. An empty array evaluates as falsey. Oftentimes you can probably substitute one for the other, but that depends on what you're trying to check.

empty is used for suppressing error reporting on otherwise undefined variables. Other than that it behaves exactly as a normal truthy/falsey check.

deceze
  • 510,633
  • 85
  • 743
  • 889