note 108768 deleted from function.isset by sobak
| From: | sobak@php.net | Date: | Sat, 10 Dec 2016 09:52:37 +0000 |
| Subject: | note 108768 deleted from function.isset by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208414@lists.php.net to get a copy of this message | ||
Note Submitter: glitch dot mr at gmail dot com
----
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.