Bug->Req #61126 [Nab]: associative array syntax
Edit report at https://bugs.php.net/bug.php?id=61126&edit=1
ID: 61126
Updated by: rasmus@php.net
Reported by: cornelius dot howl at gmail dot com
Summary: associative array syntax
Status: Not a bug
-Type: Bug
+Type: Feature/Change Request
Package: *General Issues
Operating System: Any
PHP Version: 5.4.0RC7
Block user comment: N
Private report: N
New Comment:
People are just as apt to make mistakes in key names as they are in hash names.
$_SERVER['REQUEST_URL'] when you meant to check $_SERVER['REQUEST_URI'], for
example. So I don't agree there is no value in a notice on trying to access an
array element that hasn't been defined.
I don't disagree with the notion of having a clean operator that you can use
explicitly on undefined array elements. We have a number of functions that are
specifically aimed at these. This is a valid feature request.
Previous Comments:
------------------------------------------------------------------------
[2012-02-22 06:44:54] cornelius dot howl at gmail dot com
Making new function to handle undefined key makes code slow. function call is
really slow in PHP, and wrapping with a function makes code ugly.
I mean, an undefined hash should throw an error or notice (the behavior is right).
but when using an undefined key from a defined hash, PHP should not throw an
error.
------------------------------------------------------------------------
[2012-02-18 11:45:03] nikic@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
You access an undefined index so you obviously get a notice, nothing will change about that.
If you want, you can define a function like
function getWithDefault(&$var, $default = null) {
return isset($var) ? $var : $default;
}
$abc = getWithDefault($def['abc'], 'ghi');
------------------------------------------------------------------------
[2012-02-17 17:54:01] cornelius dot howl at gmail dot com
Description:
------------
When using undefined $args['method'] this should not throw an error, instead, it
should return undef or null.
In Perl or Ruby, Python, we usually write:
$value = $hash{key} || 'default value';
But in PHP, we have to write such code, that's such inefficient:
$value = isset($hash['key']) ? $hash['key'] : 'default';
Yes, we can use '@', but the '@' makes PHP runs more slower.
It will be better if we can have the following syntax:
$value = $hash['key'] || 'default value';
so that if you have multiple hashes:
$value = $hash['key'] || $hash2['key'] || $hash3['key'] ||
'last value';
then we don't have to write:
$value = isset($hash['key']) ? $hash['key'] : isset($hash2['key'])
?
$hash2['key'] : $hash2['key'] ........ # whatever
Thanks
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61126&edit=1
Thread (4 messages)