ID:          8353
 Updated by:  [EMAIL PROTECTED]
 Reported By: [EMAIL PROTECTED]
-Status:      Feedback
+Status:      Open
 Bug Type:    Arrays related
-PHP Version: 4.3.0-dev
+PHP Version: 4.4.0-dev
 New Comment:

As I've been taught, foreach doesn't affect the pointer as it merely
works on a copy.  Also, the following note exists in the foreach()
entry of the manual and has for over two years:

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. 

That was added by Sterling here:

http://cvs.php.net/diff.php/phpdoc/en/language/control-structures.xml?login=2&r1=1.9&r2=1.10&ty=h


Everyone assumes foreach works on a copy and only a copy.  IMHO each()
isn't a fair comparison because it doesn't work on a copy.


Previous Comments:
------------------------------------------------------------------------

[2002-11-24 23:10:40] [EMAIL PROTECTED]

IMO this is not a bug. Foreach works virtually like while + each
combination, with a few exceptions such as not requiring a reset()
prior to execution since it does not care about the current position in
the array.
As far as current(), next() and simular are concerned,
while (each($arr)); == foreach ($arr as $v);, once the loops are
complete current() will return FALSE. This IMO is a consistent
behaviour and is definately not a bug. I believe this is merely a
documentation issue.

------------------------------------------------------------------------

[2002-11-21 15:21:34] [EMAIL PROTECTED]

foreach essentially REMOVES the array pointer alltogether on the
original array.

$arr = array('a','b'); foreach ($arr as $v); var_dump( current($arr)
);
$arr = array('a','b'); foreach ($arr as $v); reset($arr); var_dump(
current($arr) );

Results:
bool(false)
string(1) "a"

Using key() instead of current() results in NULL instead of false. 
Where did the pointer go?

Conclusion:
Either foreach() has a feature that removes the array pointer from the
original array or it's a bug.  If it's seen as a feature please explain
why so it can be documented.

------------------------------------------------------------------------

[2001-03-09 21:32:57] [EMAIL PROTECTED]

no feedback.


------------------------------------------------------------------------

[2001-01-05 22:52:10] [EMAIL PROTECTED]

I get 121 for both cases. Please try the latest snapshot from
http://snaps.php.net/

--Jani

------------------------------------------------------------------------

[2000-12-21 09:44:03] [EMAIL PROTECTED]

try 1.:

$var[] = "1";
$var[] = "2";

foreach($var as $test)
{
echo $test;
}
reset($var); 
echo current($var);

output -> 12

then try:
$var[] = "1";
$var[] = "2";

//reset($var);
foreach($var as $test)
{
echo $test;
current($var);
}
echo current($var);

output -> 121

this should be the output of the first script too?
Greetings lunepi

------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=8353&edit=1

Reply via email to