Skip to content

Sum of items

PHP foreach

Flowchart (ISO 5807)

StartInput itemssum = 0x ∈ itemssum = sum + xReturn sumEndFigure 1 — total

Source code

PHP
function total($items) {
    $sum = 0;
    foreach ($items as $x) {
        $sum += $x;
    }
    return $sum;
}

The editor opens with this code — change it and the chart redraws.

Language
PHP
Constructs
foreach
Topic
Loops
Formats
Word, Visio, draw.io, PDF, SVG

Related examples