Req #69626 [NEW]: how check variable be defined ? how check variable be exists

From: Date: Tue, 12 May 2015 09:42:05 +0000
Subject: Req #69626 [NEW]: how check variable be defined ? how check variable be exists
Groups: php.standards 
Request: Send a blank email to standards-+get-763@lists.php.net to get a copy of this message
From:             hark110 at 63 dot com
Operating system: 
PHP version:      5.6.8
Package:          PHP Language Specification
Bug Type:         Feature/Change Request
Bug description:how check variable be defined ? how check variable be exists

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 bug report at https://bugs.php.net/bug.php?id=69626&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=69626&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=69626&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=69626&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=69626&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=69626&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=69626&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=69626&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=69626&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=69626&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=69626&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=69626&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=69626&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=69626&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69626&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=69626&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=69626&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=69626&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=69626&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=69626&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=69626&r=mysqlcfg



Thread (1 message)

  • hark110 at 63 dot com
« previous php.standards (#763) next »