Req #60906 [NEW]: new function "udef", similar to isset or empty

From: Date: Fri, 27 Jan 2012 17:21:34 +0000
Subject: Req #60906 [NEW]: new function "udef", similar to isset or empty
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-166862@lists.php.net to get a copy of this message
From:             
Operating system: 
PHP version:      5.4.0RC6
Package:          *General Issues
Bug Type:         Feature/Change Request
Bug description:new function "udef", similar to isset or empty

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 bug report at https://bugs.php.net/bug.php?id=60906&edit=1
-- 
Try a snapshot (PHP 5.4):            https://bugs.php.net/fix.php?id=60906&r=trysnapshot54
Try a snapshot (PHP 5.3):            https://bugs.php.net/fix.php?id=60906&r=trysnapshot53
Try a snapshot (trunk):              https://bugs.php.net/fix.php?id=60906&r=trysnapshottrunk
Fixed in SVN:                        https://bugs.php.net/fix.php?id=60906&r=fixed
Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=60906&r=needdocs
Fixed in release:                    https://bugs.php.net/fix.php?id=60906&r=alreadyfixed
Need backtrace:                      https://bugs.php.net/fix.php?id=60906&r=needtrace
Need Reproduce Script:               https://bugs.php.net/fix.php?id=60906&r=needscript
Try newer version:                   https://bugs.php.net/fix.php?id=60906&r=oldversion
Not developer issue:                 https://bugs.php.net/fix.php?id=60906&r=support
Expected behavior:                   https://bugs.php.net/fix.php?id=60906&r=notwrong
Not enough info:                     https://bugs.php.net/fix.php?id=60906&r=notenoughinfo
Submitted twice:                     https://bugs.php.net/fix.php?id=60906&r=submittedtwice
register_globals:                    https://bugs.php.net/fix.php?id=60906&r=globals
PHP 4 support discontinued:          https://bugs.php.net/fix.php?id=60906&r=php4
Daylight Savings:                    https://bugs.php.net/fix.php?id=60906&r=dst
IIS Stability:                       https://bugs.php.net/fix.php?id=60906&r=isapi
Install GNU Sed:                     https://bugs.php.net/fix.php?id=60906&r=gnused
Floating point limitations:          https://bugs.php.net/fix.php?id=60906&r=float
No Zend Extensions:                  https://bugs.php.net/fix.php?id=60906&r=nozend
MySQL Configuration Error:           https://bugs.php.net/fix.php?id=60906&r=mysqlcfg



Thread (5 messages)

« previous php.bugs (#166862) next »