Checking for the presence of the $ _POST variable

Possible duplicate:
Check if $ _POST exists

I try to start something if and only if the $ _POST variable is populated.

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

+8
php
source share
3 answers

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

+21
source share

No, empty () is not suitable for this. You must use isset ().

Why? Because many things are considered empty, which you probably do not want to miss!

The following things are considered 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!

+3
source share

You can check $_SERVER['REQUEST_METHOD'] below POST or something else. See $ _ SERVER .

Oh, I completely misunderstood your question. Do you want to check a specific entry in $_POST ? Then use array_key_exists($key, $_POST) .

+1
source share

All Articles