Website membership errors after php 8 upgrade

By daniel - February 15, 2022

Hi theclicklab,

The broad reason for these errors is that in PHP 8 it throws an error if you try to use a variable as an array when it's not an array. In the first case, $CURRENT_USER can be false if a user is not logged in, and in the second case, $link_redirectsRecord will be null if nothing is returned by getRecords(). 

For issue #1, just making sure that $CURRENT_USER isn't empty should be sufficient, like this:

if (!empty( $CURRENT_USER ) && $CURRENT_USER['member_approved'] == 1 && $CURRENT_USER['private_yacht_access'] == 1 ) {

(This works because PHP won't evaluate the right side of an "&&" if the left side is false. So if $CURRENT_USER is empty, it doesn't "see" the rest of the if statement)

For issue #2, I'd suggest checking that the record exists before using it (this is a good practice to follow generally), something like this:

if (!empty( $link_redirectsRecord )) {      
  if ($link_redirectsRecord['link_destination']) {
    $linky = $link_redirectsRecord['link_destination']; // ERROR ON THIS LINE
  } else {
    $linky = $link_redirectsRecord['yachtfolio_link_destination'] . $settingsRecord['yachtfolio_id']; // ERROR ON THIS LINE
  }
}
else {
// some fallback here
}

Let me know if this helps solve the issue, or if you have any other questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

Thanks Daniel, was able to resolve everything with your examples. Much appreciated :)