Req #67446 [Opn->Nab]: Add built-in function for combining isset() with value retrieval
| From: | fa@php.net | Date: | Sun, 15 Jun 2014 09:42:41 +0000 |
| Subject: | Req #67446 [Opn->Nab]: Add built-in function for combining isset() with value retrieval | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-186198@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67446&edit=1
ID: 67446
Updated by: fa@php.net
Reported by: nate at frickenate dot com
Summary: Add built-in function for combining isset() with
value retrieval
-Status: Open
+Status: Not a bug
Type: Feature/Change Request
Package: Arrays related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same.
Thank you for your interest in PHP.
Hi,
there were extensive discussions about this in the past. All ml threads should be linked here: https://wiki.php.net/rfc/ifsetor
Previous Comments:
------------------------------------------------------------------------
[2014-06-15 03:57:49] nate at frickenate dot com
Description:
------------
The frequency at which an isset() call against an index of an array is immediately followed by
retrieving the value at that index is extremely common. It would allow for much cleaner code if
there were a built-in mechanism for combining these 2 operations into one, eliminating the need to
type of the full array notation twice.
Test script:
---------------
<?php
// code like this is commonly written; duplication of the
// item $array['foo']['bar']['baz'] is extremely annoying:
$value = isset($array['foo']['bar']['baz']) ?
$array['foo']['bar']['baz'] : 'default';
// some people avoid this by (ugh!) using @ to skip the undefined index notice:
if (($value = @$array['foo']['bar']['baz']) === null) {
$value = 'default';
}
// what we should really have (though with a better name, not sure what):
$value = iisset($array['foo']['bar']['baz'], 'default');
// this can already be implemented without risk of a notice in userland:
function iisset(&$var, $default = null) {
return isset($var) ? $var : $default;
}
// but a php core replacement would make it universal and take less overhead
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67446&edit=1