Thompson Thompson - 7 months ago 25
PHP Question

Function to see whether variable has values but it is zero

If we need to check whether the variale is set or not, we can easily check it by function isset()
But what about is_zero?

But
isset($myvar)

doesn't accomplish my task. If we use isset($myvar) then PHP would return false always and If I use
if(empty($myvar))

then I get Notice:
Notice: Undefined index: $myvar

I thought it must be there in PHP, because sometimes we need to store the zero value variables also.

I tried this:

if(is_zero($myvar))
echo $myvar;


But got this error:
Fatal error: Call to undefined function is_zero()

Maybe a bug in PHP ?

Answer

is_zero is not a PHP function, you can write your own:

function is_zero($input)
{
    if($input === 0)
        return true;
    else
        return false;
}

if there is a possibility that $input is not defined earlier, you can add & in front of $input or @ in front of is_zero to hide a possible notice error.

function is_zero(&$input)
{
    if($input === 0)
        return true;
    else
        return false;
}

or

if(@is_zero($variable))
Comments