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