Req #60944 [Opn->Wfx]: Unable to detect if variable exists vs. is null
| From: | laruence@php.net | Date: | Fri, 03 Feb 2012 09:00:05 +0000 |
| Subject: | Req #60944 [Opn->Wfx]: Unable to detect if variable exists vs. is null | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-167089@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60944&edit=1
ID: 60944
Updated by: laruence@php.net
Reported by: lampacz+php at gmail dot com
Summary: Unable to detect if variable exists vs. is null
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Variables related
Operating System: Linux
PHP Version: 5.3.9
Block user comment: N
Private report: N
New Comment:
php is a loose-type language. there is no different between a null and a
undefined variable .
as I said above, you can use isset($GLOBALS[***]) to achieve your requirement.
Previous Comments:
------------------------------------------------------------------------
[2012-02-02 02:00:19] phpmpan at mpan dot pl
What is the rationale for introducing such function? Can you provide any real-life example when it
is neccessary to distinguish between an undefined global variable and a global variable set to
NULL? The question is important, as use of global variables is getting less popular.
Adding a new function, that will duplicate already existing functionality (proposed by Laruence), is
IMHO not a good idea.
------------------------------------------------------------------------
[2012-02-01 10:39:28] lampacz+php at gmail dot com
Marvelous.
Sugestion for new function: is_defined(mixed $var)
which internally return array_key_exists($var, get_defined_vars()); ?
Thank you for.
------------------------------------------------------------------------
[2012-02-01 09:54:15] laruence@php.net
<?php
$a = null;
var_dump(array_key_exists('a', get_defined_vars())); //or $GLOBALS
?>
------------------------------------------------------------------------
[2012-02-01 08:05:10] lampacz+php at gmail dot com
Description:
------------
There is no difference between $x = NULL and unset($a). So i'm unable to detect if variable was
defined (so it can be defined and be filled with null)
http://www.php.net/manual/en/types.comparisons.php
Table Comparisons of $x with PHP functions, rows 2 - 4 gives same results
Expected result:
----------------
Different results for undefined variables and null variables
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60944&edit=1