|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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; PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 15:00:01 2025 UTC |
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.