php.net |  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
 [2015-05-12 09:42 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-12 10:52 UTC] cmb@php.net
-Status: Open +Status: Feedback -Package: PHP Language Specification +Package: Variables related
 [2015-05-12 10:52 UTC] 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 10:58 UTC] daverandom@php.net
-Status: Feedback +Status: Duplicate
 [2015-05-12 11:02 UTC] daverandom@php.net
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-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC