Edit report at https://bugs.php.net/bug.php?id=69626&edit=1
ID: 69626
Updated by: cmb@php.net
Reported by: hark110 at 63 dot com
Summary: how check variable be defined ? how check variable
be exists
-Status: Open
+Status: Feedback
Type: Feature/Change Request
-Package: PHP Language Specification
+Package: Variables related
PHP Version: 5.6.8
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[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