Req #69626 [Com]: how check variable be defined ? how check variable be exists
| From: | daverandom@php.net | Date: | Tue, 12 May 2015 11:02:27 +0000 |
| Subject: | Req #69626 [Com]: how check variable be defined ? how check variable be exists | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-192632@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69626&edit=1
ID: 69626
Comment by: daverandom@php.net
Reported by: hark110 at 63 dot com
Summary: how check variable be defined ? how check variable
be exists
Status: Duplicate
Type: Feature/Change Request
Package: Variables related
PHP Version: 5.6.8
Block user comment: N
Private report: N
New Comment:
This is definitely a dupe of the bug referenced above, and as demonstrated in the report itself
there already is a way to do it (array_key_exists/get_defined_vars).
I have never seen anyone demonstrate a use case for this where there wasn't simply a better way
to do what was being done - as a general rule, if you need to do this then you should be using an
assoc array instead.
Previous Comments:
------------------------------------------------------------------------
[2015-05-12 10:52:45] cmb@php.net
Actually, this is a duplicate of bug #69476.
Anyhow, I wonder why you have the need to distinguish undefined
variables from variables with the value NULL.
------------------------------------------------------------------------
[2015-05-12 09:42:03] hark110 at 63 dot com
Description:
------------
current php no best way check a variable whether be defined, only like below:
if check $a whether be defined
in global varibale:
array_key_exists('a', $GLOBALS);
in function or method:
array_key_exists('a',get_defined_vars())
because isset, empty is not trust, see test script
Test script:
---------------
<?php
function test() {
//defined variable
$a = null;
var_dump(isset($a)); //false
var_dump(is_null($a));//true
var_dump(empty($a)); //true
//undefined variable
var_dump(isset($b));//false
var_dump(empty($a)); //true
var_dump(@is_null($c));//true, Notice: Undefined variable: a
var_dump(get_defined_vars());//$b,$c undefined, $a is defined and value is NULL
//be unset() variable
$d;
unset($d);
var_dump(empty($a)); //true
var_dump(isset($d));//false
var_dump(@is_null($d));//true,Notice: Undefined variable: a
var_dump(get_defined_vars());//$b,$c undefined,$d be unset and defined, $a is defined and value
is NULL
//empty string variable
$e = '';
var_dump(empty($e)); //true
var_dump(isset($e)); // true
var_dump(is_null($e)); //false
var_dump(get_defined_vars());//$b,$c undefined,$d be unset and undefined, $a is defined and
value is NULL, $e is defined and value is empty string
}
test();
Expected result:
----------------
<?php
$a;
var_dump(isset($a)); // is false
Actual result:
--------------
<?php
$a;
var_dump(isset($a)); // is true
?>
or
<?php
$a;
var_dump(isdefined($a)); //is true
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69626&edit=1