|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69626 how check variable be defined ? how check variable be exists
Submitted: 2015-05-12 09:42 UTC Modified: 2015-05-12 10:58 UTC
From: hark110 at 63 dot com Assigned:
Status: Duplicate Package: Variables related
PHP Version: 5.6.8 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
26 - 13 = ?
Subscribe to this entry?

 [2015-05-12 09:42 UTC] hark110 at 63 dot com
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:

because isset, empty is not trust, see test script

Test script:

function test() {
    //defined variable
    $a = null;
    var_dump(isset($a)); //false
    var_dump(empty($a)); //true

    //undefined variable
    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
    var_dump(empty($a)); //true
    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


Expected result:
var_dump(isset($a)); // is false

Actual result:
var_dump(isset($a)); // is true

var_dump(isdefined($a)); //is true


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-12 10:52 UTC]
-Status: Open +Status: Feedback -Package: PHP Language Specification +Package: Variables related
 [2015-05-12 10:52 UTC]
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 10:58 UTC]
-Status: Feedback +Status: Duplicate
 [2015-05-12 11:02 UTC]
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.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat May 15 12:01:23 2021 UTC