php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60944 Unable to detect if variable exists vs. is null
Submitted: 2012-02-01 08:05 UTC Modified: 2012-02-03 09:00 UTC
From: lampacz+php at gmail dot com Assigned:
Status: Wont fix Package: Variables related
PHP Version: 5.3.9 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: lampacz+php at gmail dot com
New email:
PHP Version: OS:

 

 [2012-02-01 08:05 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-01 09:54 UTC] laruence@php.net
<?php
$a = null;
var_dump(array_key_exists('a', get_defined_vars())); //or $GLOBALS
?>
 [2012-02-01 10:39 UTC] 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-02 02:00 UTC] 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-03 09:00 UTC] laruence@php.net
-Status: Open +Status: Wont fix
 [2012-02-03 09:00 UTC] laruence@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 04:01:28 2024 UTC