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?
I'd do if(isset($_POST['key'])) { ... }
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!
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With