【问题标题】:PHP Warning: A non-numeric value encountered ErrorPHP 警告:遇到非数字值错误
【发布时间】:2021-06-05 09:15:52
【问题描述】:

我在日志文件中收到此错误

在第 1279 行的 /wp-content/themes/myarcadetheme/functions.php 中遇到的非数字值

这是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' );

这是在第 1279 行找到的代码。

$ratio = $width / $height;

请帮忙。

【问题讨论】:

  • 是错误还是警告?这意味着变量 $width 和 $height 中的一个或两个不是数字。它们可以是存储为文本的数字。它不应该对代码造成任何问题,因为 php 会在除法操作期间将它们转换为数字。 Php 警告在除法操作中使用了文本值,这不是一个好习惯。
  • 那么如何修复此错误,使其不会显示在错误日志中。
  • 这是一个警告,而不是错误。它对代码没有影响。如果你想避免烦恼,试试这个 $ratio = (int) $width / (int) $height;

标签: php wordpress function


【解决方案1】:

$heiht 或 $width 中的任何一个(或两者)都不是数字,可能为 null

在之前用 is_numeric 测试它们

生产中

if ( is_numeric($width) && is_numeric($width) ) $ratio = $width / $height; else $ratio=...你的默认值...;

Warning: A non-numeric value encountered

【讨论】:

  • 我不是 php 专家。你能解释一下我应该在哪里添加 is_numeric 代码吗?
  • 更正更新以避免生产中的错误,在调试中您可以停止执行并显示错误值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多