note 5869 deleted from each by ronabop

From: Date: Mon, 21 Aug 2000 09:39:57 +0000
Subject: note 5869 deleted from each by ronabop
Groups: php.notes 
Request: Send a blank email to php-notes+get-2856@lists.php.net to get a copy of this message
each() is apparently a destructive operator. The following code: <PRE> &lt;?php class foo { var $data; function foo() { $this->data = array(1, 2); } function get_data() { while (list($key, $val) = each($this->data)) { $str .= "$val "; } return $str; } } $foo1 = new foo; print $foo1->get_data()."<br>n"; print $foo1->get_data()."<br>n"; ?> </PRE> Results in the following output: 1 2 The expected output is: 1 2 1 2 Why is each() destructive? This is php version 3.0.12. PS: Replacing the following statement in the above code: <PRE> while (list($key, $val) = each($this->data)) { </PRE> with: <PRE> $test = $this->data; while (list($key, $val) = each($test)) { </PRE> produces the correct output. Thanks!

« previous php.notes (#2856) next »