|  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
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
 [2011-05-15 20:44 UTC] tulpetulpe at fastmail dot fm
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 ( 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;


Add a Patch

Pull Requests

Add a Pull Request


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 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. .

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]
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2014-10-12 14:58 UTC]
PHP 7 adds the null coalesce operator, see
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Jan 22 04:01:23 2021 UTC