sluggerdog
sluggerdog

Reputation: 843

function to check if value exists - php

I was wondering if it is possible to have a general system function that checks for a value and if it doesn't exist return another value.

below is what I have come up with however if I use it on a value say $thisvalue but $thisvalue doesn't exist I am getting the error Undefined variable: thisvalue. If thisvalue is empty or null etc below works.

Thanks

public function checkingValue($value = NULL, $empty_return = NULL)
{
    if(isset($value))
    {
        return $value;
    }
    else
    {
        return $empty_return;
    }
    if(!empty($value))
    {
        return $value;
    }
    if(!is_null($value))
    {
        return $value;
    }

    return $empty_return;
}

Upvotes: 1

Views: 9749

Answers (1)

Halcyon
Halcyon

Reputation: 57719

isset and empty are not functions, they're actually language constructs like switch and list and class.

If you do this:

function my_isset($var) {
    return isset($var);
}

isset($undef); // false
my_isset($undef); // Error: Undefined variable: $undef

Upvotes: 3

Related Questions