Page MenuHomeWickedGov Phorge

ForeachUniqueAssignmentStandard.xml
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

ForeachUniqueAssignmentStandard.xml

<?xml version="1.0"?>
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
title="Foreach Unique Assignment"
>
<standard>
<![CDATA[
When a foreach control structure uses the same variable for both the $key as well as the $value assignment, the key will be disregarded and be inaccessible and the variable will contain the value.
Mix in reference assignments and the behaviour becomes even more unpredictable.
This is a coding error. Either use unique variables for the key and the value or don't assign the key.
]]>
</standard>
<code_comparison>
<code title="Valid: using unique variables.">
<![CDATA[
foreach ($array as $k => $v ) {}
]]>
</code>
<code title="Invalid: using the same variable for both the key as well as the value.">
<![CDATA[
foreach ($array as $k => $k ) {}
]]>
</code>
</code_comparison>
</documentation>

File Metadata

Mime Type
text/xml
Expires
Mon, Nov 23, 12:30 (5 d, 10 h ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
57/76/c8dcfa039a81099279294d54199d
Default Alt Text
ForeachUniqueAssignmentStandard.xml (1 KB)

Event Timeline