0

I have seen that

isset($n=$this->myvariable) does not work

but this works

array_key_exists($t=$this->type, $m=$this->map)

also

if($n=$this->myvariable) also does not work

user3147180
  • 923
  • 3
  • 12
  • 25
  • What is `%this->myvariable`? Is it set? The last one should work. – putvande Mar 02 '14 at 16:55
  • "Why" questions are the domain of the language developers. We can describe what works and what doesn't, but any response to the "why" question will mostly be guesswork. – George Cummins Mar 02 '14 at 16:55
  • @GeorgeCummins i want to know how can i describe variable inside `isset` because sometimes i need to use that in next statement and i don't want to write long text again – user3147180 Mar 02 '14 at 17:00

1 Answers1

1

isset is specifically intended to determine if a reference to a variable, array index, or object property has be set. It needs to be passed in one of those. $n = $this->myvariable actuall evaluates to a value being assigned to $n and not the variable $n itself.

if is a language construct, not a function/method. It determine if whatever is inside it evaluates to true or false. This can be a variable or a conditional or a function call or the result of an asisgnment to name a few

array_key_exists() takes 2 arguments: the first is just about anything, the second is an array. These can be passed in explicitly by value or by their variable. For example:

  array_key_exists('123', array());

is perfectly fine, even though no variables are being created or passed in.

This is differnt with isset() as these all would error:

  isset(array()); 
  isset(1);
  isset('somestring');

as no variable is being passed in..

For once in my life, I can honestly say something like this would be easier to explain in JAVA and C where the concept of pointers and references are clearer and more prevalent :)

Ray
  • 40,256
  • 21
  • 101
  • 138