How to avoid "Undefined array key" warning?

Hi,
I have a doubt and although I’ve tried several possibilities and also searched before in the forum I still didn’t find a way to achieve it.I have a view to manage bookings through a QR code. Since the page has to be open (because not all people will have a registered user) and since the page will deal with some private information, I think that the best way to achieve it is by filtering the returned row applying a long hashed value passed through $_GET variable, let’s say “booking_id”. So, I’ve built the Filter string like:“booking_id = '” . $_GET[“code”] . “‘"So, the whole query address look likehttps://address?code=somecodeand all works as expected, returning only one row. However, since this is a GET request, if ?code=somecode is manually deleted from the addressbar (we can’t trust what we will receive…), a “Undefined array key” warning is thrown, which is fair enough, since the variable is not defined.So, my question is, is possible to pass an “if” validation inside the Filter string? Something like:if($_GET[‘code’]){ “booking_code = '” . $_GET[“code”] . "’” } else { “booking_code = NULL” }Thank you in advance for your help.

You may use PHP’s Null coalescing operator.

Thank you very much for your answer.Your link put me in the right direction because I realised I’ve tried it using “if” but “isset”… mhhh… Junior programmer fault… jajajaSo, I was able to achieve the right result and no warning using the ternary operator:isset($_GET[‘booking’]) ? (“booking_code = '” . $_GET[“booking”] . “'”) : “booking_code = NULL”;but I wasn’t able to build the string for the right side of the coalescing operator… :-((“booking_code = '” . $_GET[“booking”] . “'”) ?? (“booking_code = NULL” . “'”);This works as expected when $_GET is set, but when not, it still throws the warning…I still have some struggle with this string…

The ?? operator is for checking null only, not for empty string. In your case, using the ternary operator is fine, e.g.“booking_code " . (isset($_GET[“booking”]) ? " = '” . $_GET[“booking”] . “'” : “IS NULL”)If you really want to use ??, you may use, e.g.“booking_code " . ($_GET[“booking”] ?? false ? " = '” . $_GET[“booking”] . “'” : “IS NULL”)

How about this?isset($_GET[‘booking’]) ? (“booking_code = '” . $_GET[“booking”] . “'”) : “booking_code IS NULL”;

Thank you both very much for your proposals! They are all working flawlessly!I think that in this case I will stick with the ternary solution, since using coalescence concatenated to a ternary makes it more difficult for me to read.Possibly one final question to put the icing on the cake: would this extra layer add anything to improve the code and would it really be worth adding?

wally wrote:

Possibly one final question to put the icing on the cake: would this extra layer add anything to improve the code and would it really be worth adding?

If you meant the null coalescing operator, it is extremely useful, it is just not quite in your particular case.

Sure… I meant in this case… Sorry because my post wasn’t very clear…
Thanks again for all your help.