php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63713 Need an "UNDEFINED" type
Submitted: 2012-12-06 17:42 UTC Modified: 2012-12-07 01:51 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:0 of 1 (0.0%)
From: liquid_nitrogen_4ever at yahoo dot com Assigned:
Status: Wont fix Package: Variables related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: liquid_nitrogen_4ever at yahoo dot com
New email:
PHP Version: OS:

 

 [2012-12-06 17:42 UTC] liquid_nitrogen_4ever at yahoo dot com
Description:
------------
Consider the following scenario currently in PHP:

var_dump($notExistentVariable);
-> NOTICE: Undefined variable notHere on line 1
   NULL

isset($notExistentVariable);
-> FALSE

$foo = null;
var_dump($foo);
-> NULL

isset($foo);
-> FALSE


If there were an UNDEFINED "type" (in the same sense that there is a NULL), the 
above scenarios would change to:

var_dump($notExistentVariable);
-> UNDEFINED

isset($notExistentVariable);
-> FALSE

$foo = null;
var_dump($foo);
-> NULL

isset($foo);
-> TRUE

However, unlike NULL, you would NOT be allowed to explicitly initialize 
something to UNDEFINED.
$x=UNDEFINED;//error:  if you want it to be undefined, don't declare it.

However, having just:
$x;

makes sense (to me at least) as shortcut to:
$x=NULL;

In other words, if a variable is declared but not explicitly initialized, it 
will implicitly be set to NULL (which is 
essentially what you are already doing, but one can't really differentiate 
between declared and undeclared variables).


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-07 01:51 UTC] aharvey@php.net
I don't see much value in this: it's an unusual use case, would require engine changes, and you can already check if a variable is defined with array_key_exists('notExistentVariable', get_defined_vars()).
 [2012-12-07 01:51 UTC] aharvey@php.net
-Status: Open +Status: Wont fix
 [2013-07-31 02:41 UTC] metamarkers at gmail dot com
True, but checking for nested indices in arrays is a huge pain and often results in code duplication.

$x = [];

Implicit array expansion is great:

$x['a']['b']['c'] = 1; // works

Example where this would be useful is the ternary operator:

return isset($x['very']['dimensional']['element']) ? $x['very']['dimensional']['element'] : $default;

Why not:

return $x['very']['dimensional']['element'] ? : $default;

I could see this working for objects, too. Basically any undefined symbol would resolve to UNDEFINED.

IMO since PHP doesn't throw native exceptions for nonexistent indices, UNDEFINED should be supported. Otherwise PHP 
should throw an out of bounds exception instead of crashing, and the following code should work:

try {
    echo $x['x']['y']['z']; // doesn't exist
}
catch (Exception $e) {
    echo $e->getMessage();
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC