note 108768 added to function.isset
| From: | glitch dot mr at gmail dot com | Date: | Wed, 23 May 2012 18:02:43 +0000 |
| Subject: | note 108768 added to function.isset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188171@lists.php.net to get a copy of this message | ||
This is very simple implementation of issetor() for PHP. It works like ?? in C#, // in Perl (it can
access undefined hash keys without it nevertheless) or ? in CoffeeScript (also can access undefined
hash keys too). You shouldn't use directly $_GET['something'] (it can generate
E_NOTICEs in case somebody would try hacking your site), but isset($_GET['something']) ?
$_GET['something'] : '' is long for many people (for so common operation). This
alternative makes using superglobals easier.
<?php
function issetor(&$variable, $or = NULL) {
return $variable === NULL ? $or : $variable;
}
?>
Now you can do:
<?php
echo issetor($_GET['a']); // $_GET['a'] or NULL
echo issetor($_GET['b'], '42'); // $_GET['b'] or 42
echo issetor($_GET['c']['d']); // $_GET['c']['d'] or NULL
?>
This code doesn't generate notices in case variable doesn't exist.
----
Server IP: 69.147.83.197
Probable Submitter: 178.36.22.238
----
Manual Page -- http://www.php.net/manual/en/function.isset.php
Edit -- https://master.php.net/note/edit/108768
Del: integrated -- https://master.php.net/note/delete/108768/integrated
Del: useless -- https://master.php.net/note/delete/108768/useless
Del: bad code -- https://master.php.net/note/delete/108768/bad+code
Del: spam -- https://master.php.net/note/delete/108768/spam
Del: non-english -- https://master.php.net/note/delete/108768/non-english
Del: in docs -- https://master.php.net/note/delete/108768/in+docs
Del: other reasons-- https://master.php.net/note/delete/108768
Reject -- https://master.php.net/note/reject/108768
Search -- https://master.php.net/manage/user-notes.php