Welcome! Log In Create A New Profile

Advanced

[PHP] Null Coalesce Operator and Undefined Indexes

Posted by Jennifer 
Jennifer
[PHP] Null Coalesce Operator and Undefined Indexes
November 25, 2017 06:30PM
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
Re: [PHP] Null Coalesce Operator and Undefined Indexes
November 25, 2017 06:40PM
On Sat, Nov 25, 2017 at 12:12 PM, Jennifer <[email protected]>
wrote:

> 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
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Is it because I've wrapped the first variable with a function?

Exactly. You are attempting to pass something that doesn't exist to a
method. Not sure what else to expect.
David Harkness
Re: [PHP] Null Coalesce Operator and Undefined Indexes
November 27, 2017 09:00PM
On Sat, Nov 25, 2017 at 9:12 AM, Jennifer <[email protected]>
wrote:

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

You must use the ?? operator on the array access before passing it to your
sanitize() function.

$ua = sanitize($_SERVER['HTTP_USER_AGENT'] ?? null);

Cheers,
David
Jennifer
Re: [PHP] Null Coalesce Operator and Undefined Indexes
November 27, 2017 09:30PM
> On Nov 27, 2017, at 11:53 AM, David Harkness <[email protected]> wrote:
>
> You must use the ?? operator on the array access before passing it to your
> sanitize() function.
>
> $ua = sanitize($_SERVER['HTTP_USER_AGENT'] ?? null);

So close... yet so far away. ;) Thank you, David!

Jenni
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login