Null Coalesce Operator and Undefined Indexes
| From: | Jennifer | Date: | Sat, 25 Nov 2017 17:12:00 +0000 |
| Subject: | Null Coalesce Operator and Undefined Indexes | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-326275@lists.php.net to get a copy of this message | ||
Hello,
I'm using PHP 7.1 now and just changed some code to use the Null Coalesce Operator, like so:
$ua = sanitize($_SERVER['HTTP_USER_AGENT']) ?? null;
$request = sanitize($_SERVER['REQUEST_URI']) ?? null;
$protocol = sanitize($_SERVER['SERVER_PROTOCOL']) ?? null;
$method = sanitize($_SERVER['REQUEST_METHOD']) ?? null;
However, this morning I found this line in my error log:
[25-Nov-2017 03:51:48] PHP Notice: Undefined index: HTTP_USER_AGENT in
/home/user/public_html/scripts/index.php on line 45
This doesn't make sense to me. I thought this function was supposed to eliminate this
warning. Is it because I've wrapped the first variable with a function? How/why is this
happening?
Thanks,
Jenni