.. _structures-identicalvariablesinforeach: .. _identical-variables-in-foreach: Identical Variables In Foreach ++++++++++++++++++++++++++++++ Do not use the same variable names as a `foreach() `_ source and one of its blind variables. `Foreach() `_ makes a copy of the original data while working on it : this prevents any interference. Yet, when the source and the blind variable is the same, the source will have changed after the loop. .. code-block:: php $value) { // doSomething with $key and $value } // unusual way to end up with a name conflict foreach($a as $a => [$b, $c, $a]) { // doSomething with $a and $a, $b, $c } // classic way to use a foreach loop foreach($a as $a => $b) { // doSomething with $a and $a } // Now, after the loop, $a is an integer or a string! ?> Connex PHP features ------------------- + `foreach `_ Suggestions ___________ * Use a different name for the source of the array and the blind values Specs _____ +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Short name | Structures/IdenticalVariablesInForeach | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Rulesets | :ref:`All `, :ref:`Analyze `, :ref:`Changed Behavior ` | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Exakat since | 2.3.9 | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | PHP Version | All | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Severity | Minor | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Time To Fix | Quick (30 mins) | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Precision | Very high | +--------------+-------------------------------------------------------------------------------------------------------------------------+ | Available in | `Entreprise Edition `_, `Exakat Cloud `_ | +--------------+-------------------------------------------------------------------------------------------------------------------------+