2

As per the title: is if($_POST) reliable?

Is it reliably true even if no data was posted but the HTTP POST method was used?

Is if('post' === strtolower($_SERVER['REQUEST_METHOD'])) a more reliable method, or is it overkill?

Matt
  • 9,068
  • 12
  • 64
  • 84
  • May I ask, what is the purpose of this test? Anyway, second method is not overkill, I wouldn't rely on PHP conversions, if I were you :) – Erbureth Dec 15 '11 at 11:38
  • 2
    I usually do `if (isset($_POST['nameofsubmitbutton']))` – OptimusCrime Dec 15 '11 at 11:40
  • As Evert says, this won't detect a POST method with no posted variables - but I find it hard to imagine why, if such a situation should arise, it is still necessary to differentiate between methods. – symcbean Dec 15 '11 at 12:54

1 Answers1

5

No.. because:

array() == false

So if no data is posted, the condition will turn out false. So check against the REQUEST_METHOD. Note that it would have taken you less time to test this, than for me to type this out.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • it depends whether you want to determine whether a post request was made, or whether you want to check whether some data was actually submitted – JamesHalsall Dec 15 '11 at 13:41
  • I'd say that generally speaking it's the request method you actually care about. The fact that there's no data can be handled afterward. – Matt Dec 15 '11 at 15:18