1.2.1345. Use Variable Created Inside Loop

When a variable is created inside a loop, it should also be used in the loop. Otherwise, the variable will be overwritten by each loop, and become dead code.

<?php

foreach($a as $b => $c) {
    $c = 1;
}

?>

1.2.1345.1. Suggestions

  • Remove the variable from the loop

  • Add usage to that variable inside the loop

  • Turn the variable into a property

1.2.1345.2. Specs

Short name

Structures/UseVariableInsideLoop

Rulesets

All, Dead code

Exakat since

2.3.7

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

loop

Available in

Entreprise Edition, Exakat Cloud