0

I am getting this error on log file:

A non-numeric value encountered in /wp-content/themes/myarcadetheme/functions.php on line 1279

This is the code within functions.php:

function myarcadetheme_game_height( $height ) {
  global $post;

  if ( ! isset( $post->ID ) ) {
    return $height;
  }

  $width = get_post_meta( $post->ID, 'mabp_width', true );

  $ratio = $width / $height;

  $new_width = myrcadetheme_game_width( $width );

  $height = $new_width / $ratio;

  return $height;
}
add_filter( 'myarcade_game_height', 'myarcadetheme_game_height' );

This is the code found on line 1279.

$ratio = $width / $height;

Please help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jamesalan
  • 1
  • 1
  • Is it an error or warning? It means one or both variables $width and $height are not numbers. They can be numbers stored as text. It should not cause any issue with the code as php will convert them to number during divide operation. Php is warning that text values are used in divide operation which is not a good practice. – smartdroid Jun 05 '21 at 09:33
  • So how this error can be fixed, so that it does not show on error log. – jamesalan Jun 06 '21 at 08:35
  • It is a warning, not an error. It does not have an impact on the code. If you want to avoid the annoyance, try this $ratio = (int) $width / (int) $height; – smartdroid Jun 06 '21 at 10:15

1 Answers1

0

Either (or both) $heiht or $width is non numeric, maybe null

Test them with is_numeric before

In production

if ( is_numeric($width) && is_numeric($width) ) $ratio = $width / $height; else $ratio=...your default value...;

see Warning: A non-numeric value encountered

Dri372
  • 1,275
  • 3
  • 13
  • I am not expert in php. Can you please explain where exactly should I add is_numeric code? – jamesalan Jun 06 '21 at 08:36
  • correction updated to avoid error in production, in debug you can stop execution abd display the false value(s). – Dri372 Jun 06 '21 at 08:52