Req #60906 [Opn->Wfx]: new function "udef", similar to isset or empty
Edit report at https://bugs.php.net/bug.php?id=60906&edit=1
ID: 60906
Updated by: aharvey@php.net
Reported by: luke at cywh dot com
Summary: new function "udef", similar to isset or empty
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: *General Issues
PHP Version: 5.4.0RC6
Block user comment: N
Private report: N
New Comment:
This has previously been proposed and declined (due to the existence of userspace
equivalents) as ifsetor/coalesce. An RFC was written after the fact to summarise
the discussion, and can be found at https://wiki.php.net/rfc/ifsetor
Previous Comments:
------------------------------------------------------------------------
[2012-01-27 17:21:34] luke at cywh dot com
Description:
------------
It's usually a good practice to develop with the highest error level to
eliminate all warnings and notices.
There are some cases where you know a variable or array index won't be defined.
So in order to eliminate the notice you write something like this:
$value = isset($input['name']) ? $input['name'] : "";
I end up writing this a lot, especially with user input and templates. In many
cases it's OK and intended the variable is undefined and the value is NULL.
Another "solution" is to write this:
$value = &$input['name'];
But this only works when "&" is preceded by "=".
You could also write this:
$value = @$input['name'];
But this only prevents the error from displaying. It is sill reported by
error_get_last.
We need a simple function like isset that returns the value or NULL. It could be
used like this:
$value = udef($input['name']);
This is possible now with a user defined function (code below). But it would be
nice to have a construct like isset/empty that did this.
(Not shown in example, but perhaps it could take multiple arguments like isset
and return the first non-NULL value it finds)
Test script:
---------------
function udef(&$var)
{
return $var;
}
$one = array();
print udef($one['one']);
print_r($one);
Expected result:
----------------
Array ( [one] => )
Actual result:
--------------
Array ( [one] => )
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60906&edit=1
Thread (5 messages)