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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.