tags:

views:

57

answers:

3

i have one form which have some input box and some select box. i want to apply that nothing can be empty or blank before further activity, so i use below condition

foreach($_POST as $k=>$v)
{
    if($v=='' || $v==NULL || empty($v))
    {
        $_SESSION['errMsg']=' Please fill all the fields properly';
                    header("location:somepage.php");
                    exit;
    }
     }

now my question is:

above if is useful or not?

if not then which condition is enough to prevent blank entry $v=='' or $v==NULL or empty($v) or i have to use all of these conditions?

Thanks in advance

+5  A: 

empty() should take care of all those.

From the manual:

The following things are considered to be empty:

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

And the very handy Type-Comparison Table

Mike B
+1  A: 

Mike already described when emtpy() evaluates to true.
But note this:

  • Every value in $_POST is a string anyway, so comparing it to NULL is unnecessary.
  • If a field is allowed to have the value 0, you have to treat it differently as empty() will return true for this value too!
Felix Kling
so what i have to do sir? please suggest me fullproof idea
diEcho
+1  A: 

PHP function empty() checks for everything you asked for. You also might consider form fields being empty, if they contain only whitespaces or line breaks. Avoid this by adding trim().

foreach($_POST as $k=>$v) {
    if (empty(trim($v))) {
        //...
    }
}
Nic