|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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). PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 19:00:02 2025 UTC |
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()).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(); }