-1

Today I've created a function called is_empty(). The function is similar to already existing empty() function - it adds a few more checks that are required by my script.

However, now when I run the script and some value is not set, I get the: Notice: Undefined index notice. The default empty() function display this notice and it assumes the value is empty, is there some way to configure my function to do the same? Instead of using isset() together with is_empty()?

Thank you very much!

EDIT: My function is here:

function is_empty($value, $integer = FALSE){
    if($integer){
        return empty($value) && !is_numeric($value);
    }
    return empty($value);
}
Chris Illusion
  • 277
  • 2
  • 5
  • 18

3 Answers3

3

The notice does not come from some checks you do inside your function. The notice comes from passing some argument to your function, i.e. before the function body is actually executed. Therefore, you cannot change this by implementing the function in a different way.

Oswald
  • 31,254
  • 3
  • 43
  • 68
1

i would do something like this:

  function is_empty($value, $integer = FALSE){
    if (!empty($value)){
    if($integer !== FALSE){
        $return = preg_replace("/[^0-9]+/", "", $value);
        return empty($value) && !is_numeric($value);
    }
    return empty($value);
    }
    return FALSE;
    }
rinchik
  • 2,642
  • 8
  • 29
  • 46
1

Here is my version of is_empty function

function is_empty(&$var)
{
    return is_string($var) ? trim( $var ) == '' : empty($var);
}

The solution is to pass variable by reference.

Here is another sample like this:

custom function that uses isset() returning undefined variables when used

Hope this will help.

Community
  • 1
  • 1