Error_log:PHP警告:遇到非数值

时间:2019-08-30 22:25:18

标签: php

更新到PHP 7.3后,出现以下错误

  

PHP警告:第3702行的/functions.php中遇到的非数字值

3702行:

$total += $rating['rating_score'];

功能代码:

function airkit_get_rating( $post_id ) {

if ( is_numeric($post_id) ) {
    $rating_items = get_post_meta($post_id, 'ts_post_rating', TRUE);
    if ( isset($rating_items) && is_array($rating_items) && !empty($rating_items) ) {
        $total = '';
        foreach($rating_items as $rating) {
            $total += $rating['rating_score'];
        }
        if ( $total > 0 ) {
            $round = intval($total) / count($rating_items);
            $result = round($round, 1);

            if ( is_int($round) ) {
                if ( $round == 10 ) return $result;
                else return $result . '.0';
            } else {
                return $result;
            }
        } else {
            return;
        }
    }
} else {
    return;
}}

我没有PHP经验。谁能帮助我纠正此错误?

1 个答案:

答案 0 :(得分:4)

更改

$total = '';

$total = 0;

您正在尝试将数字添加到字符串中,但是该字符串看起来并不像数字。空字符串将转换为数字0,因此您可以获得正确的总数,但也会产生警告。