Re: foreach loop

From: Date: Sat, 07 Jul 2001 21:54:30 +0000
Subject: Re: foreach loop
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-56681@lists.php.net to get a copy of this message
At 10:31 AM -0700 7/7/01, John Meyer wrote:
When I go through a foreach loop, are the values of the individual items in the array changed? For instance: $variables = array($author_firstname, $author_lastname, $author_dob, $author_dod, $author_bio); foreach($variables as $value) {
        $value = strip_tags($value);
        $value = AddSlashes($value);
} will the values be changed outside of the loop?
No, because $value is a copy the current array element, not simply a pointer to it. Furthermore, if I understand the documentation (see below) correctly, this construct (using the $variables array from above) - $i = 0; foreach ($variables as $value) { $variables[$i] =AddSlashes( strip_tags($value));
        $i++;
} - will not work either, since foreach() operates a COPY of the array. From the docs at http://www.php.net/manual/en/control-structures.foreach.php : Note: When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop. Note: Also note that foreach operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified as with the each() construct and changes to the array element returned are not reflected in the original array. - steve
John Meyer john_meyer@geocities.com Programmer If we didn't have Microsoft, we'd have to blame ourselves for all of our programs crashing
-- +------ Factoid: Of the 100 largest economies in the world, 51 are ------+ | Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ | +--- corporations ------ http://www.ips-dc.org/reports/top200text.htm ---+

« previous php.general (#56681) next »