1.2.303. Could Use array_sum()

These loops could use array_sum(). array_sum() loops over the array and sum all of its elements. It is a native PHP function, faster to execute and easier to read.

When the added elements are, in fact, arrays, use array_merge() instead of array_sum().

This is a micro-optimisation : it will speed up the code, but won’t bring large improvements.

<?php

$total = 0;
foreach($array as $b) {
    $total = $total + $b;
}

?>

1.2.303.1. Suggestions

  • Replace the loop with a call to array_sum().

1.2.303.2. Specs

Short name

Structures/CouldUseArraySum

Rulesets

All, Suggestions

Exakat since

2.3.6

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

array

Available in

Entreprise Edition, Exakat Cloud