vrana Fri Sep 2 08:32:09 2005 EDT
Modified files:
/phpdoc/en/language types.xml
Log:
Modifying array values (bug #34220)
http://cvs.php.net/diff.php/phpdoc/en/language/types.xml?r1=1.153&r2=1.154&ty=u
Index: phpdoc/en/language/types.xml
diff -u phpdoc/en/language/types.xml:1.153 phpdoc/en/language/types.xml:1.154
--- phpdoc/en/language/types.xml:1.153 Tue Apr 12 03:40:55 2005
+++ phpdoc/en/language/types.xml Fri Sep 2 08:32:09 2005
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
-<!-- $Revision: 1.153 $ -->
+<!-- $Revision: 1.154 $ -->
<chapter id="language.types">
<title>Types</title>
@@ -1925,27 +1925,25 @@
</example>
<para>
- Note that it is currently not possible to change the values of the array
- directly in such a loop.
- <!--
- Should be made possible, if you write:
- foreach ( $colors as &$color )
-
- See bug#3074
- -->
- A workaround is the following:
+ Changing values of the array directly is possible since PHP 5 by passing
+ them as reference. Prior versions need workaround:
<example id="language.types.array.examples.changeloop">
<title>Collection</title>
<programlisting role="php">
<![CDATA[
<?php
+// PHP 5
+foreach ($colors as &$color) {
+ $color = strtoupper($color);
+}
+unset($color); /* ensure that following writes to
+$color will not modify the last array element */
+
+// Workaround for older versions
foreach ($colors as $key => $color) {
- // won't work:
- //$color = strtoupper($color);
-
- // works:
$colors[$key] = strtoupper($color);
}
+
print_r($colors);
?>
]]>