svn: /phpdoc/en/trunk/language/control-structures/ foreach.xml
| From: | Nikita Popov | Date: | Sat, 12 Nov 2011 13:51:11 +0000 |
| Subject: | svn: /phpdoc/en/trunk/language/control-structures/ foreach.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-9030@lists.php.net to get a copy of this message | ||
nikic Sat, 12 Nov 2011 13:51:11 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=319096
Log:
Followup to Doc Bug #60255: Inaccurate statement about foreach copying arrays
Bug: https://bugs.php.net/60255 (Closed) Inaccurate
statement about foreach copying arrays
Changed paths:
U phpdoc/en/trunk/language/control-structures/foreach.xml
Modified: phpdoc/en/trunk/language/control-structures/foreach.xml
===================================================================
--- phpdoc/en/trunk/language/control-structures/foreach.xml 2011-11-12 10:36:55 UTC (rev 319095)
+++ phpdoc/en/trunk/language/control-structures/foreach.xml 2011-11-12 13:51:11 UTC (rev 319096)
@@ -5,11 +5,11 @@
<title><literal>foreach</literal></title>
<?phpdoc print-version-for="foreach"?>
<para>
- The <literal>foreach</literal> construct simply gives an easy way to
- iterate over arrays. <literal>foreach</literal> works only on arrays (and objects),
and
- will issue an error when you try to use it on a variable with a different
- data type or an uninitialized variable. There are two syntaxes; the
- second is a minor but useful extension of the first:
+ The <literal>foreach</literal> construct provides an easy way to
+ iterate over arrays. <literal>foreach</literal> works only on arrays
+ and objects, and will issue an error when you try to use it on a variable
+ with a different data type or an uninitialized variable. There are two
+ syntaxes:
<informalexample>
<programlisting>
<![CDATA[
@@ -23,19 +23,18 @@
</para>
<simpara>
The first form loops over the array given by
- <literal>array_expression</literal>. On each loop, the value of
+ <literal>array_expression</literal>. On each iteration, the value of
the current element is assigned to <literal>$value</literal> and
the internal array pointer is advanced by one (so on the next
- loop, you'll be looking at the next element).
+ iteration, you'll be looking at the next element).
</simpara>
<simpara>
- The second form does the same thing, except that the current
- element's key will be assigned to the variable
- <literal>$key</literal> on each loop.
+ The second form will additionally assign the current element's key to
+ the <literal>$key</literal> variable on each iteration.
</simpara>
<simpara>
- As of PHP 5, it is possible to
- <link linkend="language.oop5.iterations">customize objects iteration</link>
too.
+ It is possible to
+ <link linkend="language.oop5.iterations">customize object iteration</link>.
</simpara>
<para>
<note>
@@ -46,18 +45,16 @@
<function>reset</function> before a <literal>foreach</literal>
loop.
</para>
- </note>
- </para>
- <para>
- <note>
<para>
- <literal>foreach</literal> operates on a copy of the array values unless
- <literal>$value</literal> is <link
linkend="language.references">referenced</link>
- by preceding it with &. <literal>foreach</literal>
- has some side effects on the array pointer. Don't rely on the array
- pointer during or after the <literal>foreach</literal> without resetting it.
+ As <literal>foreach</literal> relies on the internal array pointer changing it
+ within the loop may lead to unexpected behavior.
</para>
</note>
+ </para>
+ <para>
+ In order to be able to directly modify array elements within the loop precede
+ <literal>$value</literal> with &. In that case the value will be assigned by
+ <link linkend="language.references">reference</link>.
<informalexample>
<programlisting role="php">
<![CDATA[