Edit report at https://bugs.php.net/bug.php?id=60906&edit=1
ID: 60906 User updated by: luke at cywh dot com Reported by: luke at cywh dot com Summary: new function "udef", similar to isset or empty Status: Wont fix Type: Feature/Change Request Package: *General Issues PHP Version: 5.4.0RC6 Block user comment: N Private report: N New Comment: 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; Previous Comments: ------------------------------------------------------------------------ [2012-01-30 17:21:39] 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 12:28:11] ahar...@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-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