Edit report at https://bugs.php.net/bug.php?id=54738&edit=1

 ID:                 54738
 Comment by:         michaelclark at zagg dot com
 Reported by:        tulpetulpe at fastmail dot fm
 Summary:            Shorter or more eadable solution to get possibly
                     undefined variables
 Status:             Open
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 PHP Version:        5.3.6
 Block user comment: N
 Private report:     N

 New Comment:

See https://wiki.php.net/rfc/ifsetor for more information than is useful to 
provide inline about what is fundamentally the same thought. It is worth noting 
that this RFC has been rejected before. The patch to support it should provide 
some guidance for a similar feature should it be proposed. nb. 
http://www.php.net/~derick/meeting-notes.html#ifsetor-as-replacement-for-foo-isset-foo-foo-something-else
 .

Note that userland solution #2 doesn't have the same problems if we don't _set_ 
the variable - E_NOTICE isn't thrown when passing the variable by reference, 
and the other issues are somewhat minor. So:

function ifset(&$variable) {
    return isset($variable)?$variable:null;
}


Previous Comments:
------------------------------------------------------------------------
[2011-09-16 20:33:45] michaelclark at zagg dot com

One challenge to the argument is that for userspace programmers, the proposed 
ifset is functionally almost identical to:

echo @$_REQUEST['foo'];

Personally I do not like the look of that very much. I don't trust it, in part 
because I do not know everything that the error suppression operator will 
suppress.

The functionality of dropping the E_NOTICE on $a?:$b could be relatively easily 
duplicated using ifset, like:

echo ifset($_REQUEST['foo']) ?: NULL;

Which would make ifnset unnecessary.

My primary reason for supporting this is that at times the variable being 
tested for can be very long and bothersome to type twice (or select in order to 
copy and paste); ie 
$_SESSION['module_scope']['controller_scope']['group_scope']['keyItselfWhichMayNotBeShort'].
 Typically, the longer it is, the less likely it is to be set, as any one of 
the keys leading up to it might not be set also. Due to the convenience factor 
of this feature, it might not be a wasted effort.

------------------------------------------------------------------------
[2011-05-15 20:44:34] tulpetulpe at fastmail dot fm

Description:
------------
At the moment to read a possibly undefined variable without triggering an 
E_NOTICE you have to do something like this:

echo isset($_REQUEST['foo']) ? $_REQUEST['foo'] : NULL;

I would like to use a shorter version (or at least write the variable name only 
once) to get the code more clean.

A solution could be to introduce a construct like:

echo ifset($_REQUEST['foo']);

The ifset() would do the same like the first example.

In the comment to a similar feature request 
(http://bugs.php.net/bug.php?id=43236) it is recommended to use the new ternary 
?: operator like:

echo $_REQUEST['foo'] ?: NULL;

However that is triggering an E_NOTICE too at the moment.

I'm not sure if it would be a good idea, but maybe, as an alternative solution 
to the ifset() construct, the ?: operator could be enhanced to raise no 
E_NOTICE in such situations?


Of course the solution in the first example is running and okay. So in the end 
we are talking about a "nice to have".
However, because I see a lot of places where this could make code shorter and 
more readable I would really like to see such a function or enhancement.

Test script:
---------------
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

echo $_REQUEST['foo'];
echo $_REQUEST['foo'] ?: NULL;




------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=54738&edit=1

Reply via email to