1.2.475. Foreach Reference Is Not Modified

Foreach statement may loop using a reference, especially when the loop has to change values of the array it is looping on.

In the spotted loop, reference are used but never modified. They may be removed.

<?php

$letters = range('a', 'z');

// $letter is not used here
foreach($letters as &$letter) {
    $alphabet .= $letter;
}

// $letter is actually used here
foreach($letters as &$letter) {
    $letter = strtoupper($letter);
}

?>

1.2.475.1. Connex PHP features

1.2.475.1.1. Suggestions

  • Remove the reference from the foreach

  • Actually modify the content of the reference

1.2.475.1.2. Specs

Short name

Structures/ForeachReferenceIsNotModified

Rulesets

All, Analyze, CE, CI-checks, Changed Behavior

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Examples

Dolibarr, Vanilla

Available in

Entreprise Edition, Community Edition, Exakat Cloud