-1
function myCheck($in)
      { return isset($in); }

$var1='Something';
$var2='$var1';
$var3='$varNonExitant';

What I'm trying to achive is to use myCheck to evaluate the existance of the content like this:

myCheck($var2) return true;
myCheck($var3) return false;
vitorsdcs
  • 682
  • 7
  • 32

2 Answers2

6

isset() is not really a function: it's a language construct. As such, it's allowed to do some magic that's not available to regular functions, such as being fed with non-existing variables.

To sum up: you cannot replicate it with a custom function.

Edit:

As DaveRandom pointed out in a comment below, all you can do is come close by checking if a variable isset for example:

function variable_isset(&$variable = NULL) {
    return isset($variable);
}

This approach offers two drawbacks though:

  1. This works by passing the unset variable by reference, thus creating it when called. As it's NULL it is still not set.

  2. It'll trigger an Undefined variable notice if the variable does not exist, ruining the whole concept of gracefully handling optional variables.

Most likely this should not be needed. So question remains why you can not use isset in the first place which would be much more needed to give you better guidance.

Community
  • 1
  • 1
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • since when is isset() not a function? its listed in the php docs under functions! – Nicholas King Aug 12 '13 at 09:33
  • 2
    @Nicholas It says so right there on its manual page... – deceze Aug 12 '13 at 09:35
  • @deceze I stand corrected! – Nicholas King Aug 12 '13 at 09:37
  • 3
    It's worth noting that it *is* possible to replicate `isset()` in the manner shown above - with a caveat. `function wrapped_isset(&$var = null) { return isset($var); }`. This functions exactly like `isset()` and will always return the same results, *however* when the variable didn't exist it will be created an initialised with a value of `null`. It will still return `false` for `isset()` even after this, but it will have been created and will appear, for example, in the list returned by `get_defined_vars()`. However, this is not something I would recommend as (to me at least) it is code smell. – DaveRandom Aug 12 '13 at 10:55
-2

When you cyall myCheck($abc), with set $abc = 123, it gets myCheck(123). It isn't any valid argument for isset.

You have to give the function a string with variable name:

function isset_global($variable_name)
{
    return isset($GLOBALS[$variable_name]); 
}

Of course, I am also wondering, why to do this, but it answers the question as long as you check a global variable.

hakre
  • 193,403
  • 52
  • 435
  • 836
Liglo App
  • 3,719
  • 4
  • 30
  • 54