How to fix Trying to access array offset on value of type bool in

1k views Asked by At

In PHP 7.4 i'm getting this notice: Trying to access array offset on value of type bool in

if( $book_font ){
   foreach ( $book_font as $key => $font ) {
      if ( in_array( $font['face'], $all_google_fonts ) ) {
         self::options_typography_enqueue_google_font( $font['face'] );    
      }
   }
}

Notice for this line:

if ( in_array( $font['face'], $all_google_fonts ) ) { 
1

There are 1 answers

0
badrul On

The error means in foreach loop, one or more value of $font is a boolean instead of an array.

One way to fix it is by using null coalescing operator ?? like this:

if ( in_array( $font['face'] ?? '', $all_google_fonts ) ) {