0

I upgraded my server to PHP 8.1 and I am now getting an error 'Trying to access array offset on value of type bool'

The error is referenceing the first line of the following code.

if ($email_hosting_service['value'] == 'noemail') {
    $email_hosting_service_price = 0;
} else {
    $email_hosting_service_price = $webemail;
}

I asked chatGPT if it could fix it for me and it gave me the following code to try, which unfortunately didn't work.

if ($email_hosting_service['value'] === 'noemail') {
    $email_hosting_service_price = 0;
} else {
    $email_hosting_service_price = $webemail ?? 0;
}

Any suggestions how I can fix this?

EDITED:

I added isset() to check to see if there is a value in $email_hosting_service and that seems to satisfy the PHP gods. This also worked in another area with a similar area with a similar error.

So my new code is:

if ( isset($email_hosting_service['value']) == 'noemail') {
    $email_hosting_service_price = 0;
} else {
    $email_hosting_service_price = $webemail;
}

EDITED AGAIN:

Thanks to some super helpful suggestions in the comments my final code (for now) is:

if ( isset($email_hosting_service['value']) == false ) {
    $email_hosting_service_price = 0;
} else if (isset($email_hosting_service['value']) && $email_hosting_service['value'] == 'noemail') {
    $email_hosting_service_price = 0;
} else {
    $email_hosting_service_price = $webemail;
}
ksletten
  • 1
  • 2
  • 2
    Did you search first? `$email_hosting_service` is evidently not an array then. Does this answer your question? [Trying to access array offset on value of type bool in PHP 7.4](https://stackoverflow.com/questions/59674903/trying-to-access-array-offset-on-value-of-type-bool-in-php-7-4) – Markus AO Jun 05 '23 at 17:26
  • Yes I searched first and saw that question. It seems to be saying the same thing - add a null coalescing operator just ?? 'default value' instead of 0. That did not resolve the error either though. I can't figure out how to check if it is an array first. Also this code worked fine in 7.4 so I don't understand how it not being an array is causing a problem now. – ksletten Jun 05 '23 at 17:36
  • Your issue isn't with `$webemail`. Your _actual_ problem is prior to this code, in determining why `$email_hosting_service` is not an array. Attempting to access a bool (probably `false`) as an array produces a notice in PHP 7.4 too, see https://3v4l.org/7W2DJ#v7.4.33 -- the only difference with PHP 8.2 is that it is now a _warning_ instead of a _notice_. Either one of those means you have code to fix. – Markus AO Jun 05 '23 at 17:55
  • Oh okay that makes sense warning vs notice. So I actually was able to fix this by using isset() to see if there's a value in the field. IDK if this is the best way to do this (probably not) but it is working now so I can move on to the bigger problem(s). – ksletten Jun 05 '23 at 18:22
  • `isset()` returns `true` or `false`, so your code is now `if (true == 'noemail') { ... }`, which I doubt is what you intended... Maybe `if (isset($email_hosting_service['value']) && $email_hosting_service['value'] == 'noemail') { ... }`? – Tim Lewis Jun 05 '23 at 18:29
  • 1
    You can see in this [code example](https://3v4l.org/qScEq) that if you **ONLY** use `isset()`, you get the wrong results. `isset() && ... == 'noemail'` is the correct way to go. – Tim Lewis Jun 05 '23 at 18:46
  • 1
    @TimLewis or, `if (empty($email_hosting_service['value']) || $email_hosting_service['value'] == 'noemail')`, assuming OP wants to have `0` as default for both "noemail" case and the case where he has invalid data. Really though, should fix the code and ensure that an array is provided for the conditional. – Markus AO Jun 05 '23 at 20:00
  • Oh wow, thank you so much for writing this all out. And forgive me as I don't yet understand how to post inline code so I keep trying to edit my comment like a real perfectionist. There will be cases where `$email_hosting_service` will be undefined, and technically I think it should be set to 0, but I don't think it matters because if it's not declared the variable set here is never used, but just to make it clear could I have the first if statement be `if ( isset($email_hosting_service['value']) == false )` and then the rest like you are suggesting? – ksletten Jun 05 '23 at 20:02
  • @MarkusAO Yes of course, lots of different, syntactially similar ways to check, but ultimately, since we can't see where `$email_hosting_service` is being defined, we can only really suggest run-time ways to check it. And @ksletten, you've got the correct idea, and you know how `isset()` functions, so you should be able to tweak your code to make it set the correct value for the given scenarios. – Tim Lewis Jun 05 '23 at 20:02
  • @Markus Yeah I get what you're saying and maybe someday I will be able to really fix this huge mess of code that I have inherited but for now I can only just put my fingers in the holes. – ksletten Jun 05 '23 at 20:08
  • That's not bad, but can be simplified. If you have multiple "branches" of an `if ...` statement that do the same thing, you can condense them: `if (!isset($email_hosting_service['value']) || $email_hosting_service['value'] == 'noemail') { $email_hosting_service_price = 0; } else { $email_hosting_service_price = $webemail; }`, or use a [Ternary Operator (scroll a bit)](https://www.php.net/manual/en/language.operators.comparison.php). [Code exmaple](https://3v4l.org/6VloF); all 6 Scenarios (3 for when `$email_hosting_service` is not set, 3 for when it is set to `'noemail'`) return `0` – Tim Lewis Jun 05 '23 at 20:22
  • 1
    Or, `if ($email_hosting_service['value'] ?? 'noemail' === 'noemail')` -- if we must use the null-coalescing operator, let's coalesce to `noemail` then! Yeah plug the holes first, but make sure the logic is sound in cases where unexpected values are provided. – Markus AO Jun 05 '23 at 20:36
  • Oh yeah, even better! I prefer Tim's with the OR operator because I can more quickly understand what's happening. But this has been incredibly helpful! Thank you both so much. Now I only have about 50 more errors to go! And Markus I mean this with no ill intent - don't assume all programmers are "he", some of us are "she." ;) – ksletten Jun 05 '23 at 21:11
  • There's an argument to be made for code conciseness vs code readability; use the one that fits your understanding the best, and improve it if (or hopefully when) you feel comfortable doing so. The code I would actually use here is _wildly_ different than what was suggested, but that's neither here nor there. As always, happy to help, and good luck with the other errors – Tim Lewis Jun 05 '23 at 21:30

0 Answers0