php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54738 Shorter or more eadable solution to get possibly undefined variables
Submitted: 2011-05-15 20:44 UTC Modified: 2014-10-12 14:58 UTC
Votes:5
Avg. Score:3.8 ± 0.7
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:2 (50.0%)
From: tulpetulpe at fastmail dot fm Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.6 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
49 - 34 = ?
Subscribe to this entry?

 
 [2011-05-15 20:44 UTC] 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;



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-09-16 20:33 UTC] 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-09-16 22:22 UTC] michaelclark at zagg dot com
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;
}
 [2011-09-16 22:42 UTC] michaelclark at zagg dot com
Sorry, made it simpler when I realized that PHP sets the variable (to null, of course) when referenced in this manner.

function ifset(&$variable) { return $variable; }

Note that indeed the variable is created in the current scope - get_defined_vars() will tell you that. This might not be a problem in your usecase, in which case, it should serve for your needs.
 [2014-10-12 14:58 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2014-10-12 14:58 UTC] nikic@php.net
PHP 7 adds the null coalesce operator, see https://wiki.php.net/rfc/isset_ternary.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 21:01:29 2024 UTC