note 108836 added to function.isset
| From: | DanielKlein | Date: | Tue, 29 May 2012 03:49:37 +0000 |
| Subject: | note 108836 added to function.isset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188275@lists.php.net to get a copy of this message | ||
How to test for a variable actually existing, including being set to null. This will prevent errors
when passing to functions.
<?php
// false
var_export(
array_key_exists('myvar', get_defined_vars())
);
$myvar;
// false
var_export(
array_key_exists('myvar', get_defined_vars())
);
$myvar = null;
// true
var_export(
array_key_exists('myvar', get_defined_vars())
);
unset($myvar);
// false
var_export(
array_key_exists('myvar', get_defined_vars())
);
if (array_key_exists('myvar', get_defined_vars())) {
myfunction($myvar);
}
?>
Note: you can't turn this into a function (e.g. is_defined($myvar)) because get_defined_vars()
only gets the variables in the current scope and entering a function changes the scope.
----
Server IP: 69.147.83.197
Probable Submitter: 202.59.29.222
----
Manual Page -- http://www.php.net/manual/en/function.isset.php
Edit -- https://master.php.net/note/edit/108836
Del: integrated -- https://master.php.net/note/delete/108836/integrated
Del: useless -- https://master.php.net/note/delete/108836/useless
Del: bad code -- https://master.php.net/note/delete/108836/bad+code
Del: spam -- https://master.php.net/note/delete/108836/spam
Del: non-english -- https://master.php.net/note/delete/108836/non-english
Del: in docs -- https://master.php.net/note/delete/108836/in+docs
Del: other reasons-- https://master.php.net/note/delete/108836
Reject -- https://master.php.net/note/reject/108836
Search -- https://master.php.net/manage/user-notes.php