9

Possible Duplicate:
Check if $_POST exists

I'm trying to run something if and only if a $_POST var is populated.

Can I do if(empty($_POST[...])) { ... }? Or should I go about this another way?

Community
  • 1
  • 1
AKor
  • 8,550
  • 27
  • 82
  • 136
  • What do you mean by "populated"? If `$_POST['key']` contains an empty string, is it populated or not? – GodsBoss May 07 '11 at 19:33
  • i would use if(isset($_POST['uid']) && isset($_POST['authtoken']) && !empty($_POST['uid'])&& !empty($_POST['authtoken'])){ } – Ayush P Gupta Apr 11 '18 at 16:08

3 Answers3

23

I'd do if(isset($_POST['key'])) { ... }

Rifat
  • 7,628
  • 4
  • 32
  • 46
4

No, empty() is not the right way of doing it. You have to use isset().

Why? Because many things are considered empty which you probably don't want to miss!

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

See the manual!

markus
  • 40,136
  • 23
  • 97
  • 142
2

You can check $_SERVER['REQUEST_METHOD'] wether it is POST or something else. See $_SERVER.

Ooops, I totally misread your question. Do you want to test for a specific entry in $_POST? Then use array_key_exists($key, $_POST).

GodsBoss
  • 1,019
  • 8
  • 9