uneeb uneeb - 5 days ago 5
PHP Question

checking zero is null or not in php

i am working on validation and comparisons!! i have a field that can contain the value

$val=0
or
$val="some-value"
or
$val=""
or $val=0
basically i want the
$val="0"
or
$val=0
to be validated as true..

if($val){
//works for $val="some-value"
//doesnot work for $val=0 or $val="0";
} else
{
//works corrent for $val=""
}


one conditional approach i used is

$val="";

if($val || $val==0){
echo "true";
}
else
{
//should be false but it is true
echo "false";
}

Answer

did you try this?

$val = "";

if ($val == '0') {
    echo "TRUE";
        # code...
    }   
elseif ($val == "") {
        echo "FALSE";
    }   
Comments