2
votes

After updating to PHP 7.3 I get the following error

PHP Warning: A non-numeric value encountered in /functions.php on line 3702

Line 3702:

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

Function code:

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;
}}

I have no experience in PHP. Can anyone help me to correct this error?

1
You should initialize $total to 0, not ''. - Barmar
You're trying to add a string and a number. It will automatically convert the string to a number, but now it generates a warning. - Barmar
Actually, this isn't a new warning, I reproduced it in PHP 5. The upgrade probably changed your default error reporting level. - Barmar

1 Answers

4
votes

Change

$total = '';

to

$total = 0;

You're trying to add a number to a string, but the string doesn't look like a number. The empty string is converted to the number 0, so you get the correct total, but it also produces a warning.