Req #54738 [Com]: Shorter or more eadable solution to get possibly undefined variables

From: Date: Fri, 16 Sep 2011 20:33:45 +0000
Subject: Req #54738 [Com]: Shorter or more eadable solution to get possibly undefined variables
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-163621@lists.php.net to get a copy of this message
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: 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. Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#163621) next »