-4

Can anyone tell my why the variable defined by string does not exists?

$string = 'variable';
${$string} = NULL;

echo $variable;

Variable $variable is not defined.

Jazi
  • 6,569
  • 13
  • 60
  • 92
  • 2
    From the manual : `Returns TRUE if var exists and has value other than NULL, FALSE otherwise. ` [isset](http://php.net/manual/en/function.isset.php) – DarkBee May 17 '15 at 10:10
  • Mkay, but why the variable does not exists? – Jazi May 17 '15 at 10:11
  • @KrzysztofTrzos ^ Did you read it? **and has value other than NULL** your variable is NULL, so it's false – Rizier123 May 17 '15 at 10:11
  • I know how ISSET works. I only want to know WHY the variable is not defined, when I define it by the way like i showed? – Jazi May 17 '15 at 10:12
  • Because `null` is a non-existant value – DarkBee May 17 '15 at 10:13
  • NULL is not variable.. – user1844933 May 17 '15 at 10:13
  • if you want use NULL as variable enclose it with single quotes or double... – user1844933 May 17 '15 at 10:15
  • http://php.net/manual/en/language.types.null.php – user1844933 May 17 '15 at 10:16
  • @user1844933 what are you talking about? If you want to use `null` use it as `null` and not as a string... `if ($var === null) doSthing();` – DarkBee May 17 '15 at 10:18
  • @DarkBee What would like to say, OP may want to use variable as blank or value of NULL, anyway its complicated to understand and to say ans... so that so many down voted.... – user1844933 May 17 '15 at 10:23
  • I use `null` all the time in my code. Nothing wrong with that. The downvotes are there because OP did not read the manual proper/did not ask the question properly. – DarkBee May 17 '15 at 10:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78008/discussion-between-user1844933-and-darkbee). – user1844933 May 17 '15 at 10:27
  • As per your updated question, `echo $variable;` should definitely not give you undefined `$variable` error because it is defined in the previous line. – Ulver May 17 '15 at 10:45

3 Answers3

2

According to the documentation

isset — Determine if a variable is set and is not NULL

Since you set the variable to NULL like

$string = 'variable';
${$string} = NULL;

it will return false

AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
0

As pointed by others, isset() checks if the variable is set and is not null.

If you want to check if the variable is defined at all including the ones set as null, you can use get_defined_vars() to get list defined variables and check if the variable name is there:

$string = null;
$string2 = '';

var_dump(array_key_exists('string', get_defined_vars())); // bool(true)
var_dump(array_key_exists('string2', get_defined_vars())); // bool(true)
var_dump(array_key_exists('string3', get_defined_vars())); // bool(false)
Ulver
  • 905
  • 8
  • 13
-1

An undefined variable in PHP has value NULL, therefore:

${$string} == $variable;
${$string2} == $variable2;

${$string2} == $variable2 == 'value'; // so isset returns TRUE
$variable == NULL; // so isset returns FALSE
${$string} == $variable == NULL; // so isset returns FALSE
thodic
  • 2,229
  • 1
  • 19
  • 35