php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60906 new function "udef", similar to isset or empty
Submitted: 2012-01-27 17:21 UTC Modified: 2012-01-30 18:53 UTC
From: luke at cywh dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.4.0RC6 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: luke at cywh dot com
New email:
PHP Version: OS:

 

 [2012-01-27 17:21 UTC] 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] => )

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-01-30 12:28 UTC] aharvey@php.net
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
 [2012-01-30 12:28 UTC] aharvey@php.net
-Status: Open +Status: Wont fix
 [2012-01-30 17:21 UTC] luke at cywh dot com
This is what I've gathered from the RFC link:

- The last discussion recorded in the RFC is 2004 (8 years later)
- This failed mainly because people could not agree on a name

I saw some discussion about adding "?:" and making the first part not emit a 
notice. That obviously isn't the case 
as a notice is emitted (thankfully).

I also saw some mention of it being difficult as it would make the engine slow. 
But this discussion took place 8 
years ago. A lot of major changes have happened to the engine since then.

There was also mention that this function would check for existence, making a 
distinction between existence and 
NULL. While there still is a difference, isset() treats NULL as being non-
existant, at least it has since PHP 5. 
This function should behave the same way.

And yes it is possible in user land, but the implementation is limited. User 
land functions are also slower. If you 
do not explicitly declare a parameter with a "&" it is not passed by reference. 
So in user land you must have a 
limited numer of parameters, unlike isset.

If this were to be implemented it should be exactly like isset, the difference 
being that it returns the first non-
null parameter it finds.

Personally I could care less about the name. I care more about the 
functionality. Is there a chance at all to re-
open this discussion?
 [2012-01-30 18:53 UTC] luke at cywh dot com
Nevermind. Found some topics on making a special ternary operator last year. Looks 
like nobody can agree on anything. I guess there's no hope at all on this.

Something like this would be cool:

$foo = (set)$bar;
 [2013-12-29 22:23 UTC] info-php at swissbusinet dot com
Each time where to process forms or data base access these isset() novels make the code very ugly.
Many years ago, Oracle has invented the SQL-function NVL( 1st_var, 2nd_var, 3rd_var, ...) which pic up the first non-Null value out of the list.
This is very elegant and solves the need of keeping the code readable.
Yes - the name "NVL" is not very sexy, Null-Value-List, feel free to define something better. I personally something like this: $result ?= ($a,$b,$c), but NVL also would be fine. 
The advantage is that this function construction fit all request in this topic as I found on different pages.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 21:01:28 2024 UTC