From:             disposable_address at kennel17 dot co dot uk
Operating system: Windows
PHP version:      5.2.11
PHP Bug Type:     Scripting Engine problem
Bug description:  foreach() affects array pointer unexpectedly (different 
between PHP versions)

Description:
------------
The array pointer does not behave in the expected manner within a
foreach() loop.  What's more, the behaviour differs between PHP4 and PHP5.



Reproduce code:
---------------
$arr = array(1, 2, 3);

end($arr);

var_dump(current($arr));
foreach ($arr as $Item) {
print("Item " . $Item . " - ");
var_dump(current($arr));
}

var_dump(current($arr));


Expected result:
----------------
EITHER the array pointer IS NOT affected by the loop:

  int(3)
  Item 1 - int(3)
  Item 2 - int(3)
  Item 3 - int(3)
  int(3)

OR the array point IS affected by the loop:

  int(3)
  Item 1 - int(1)
  Item 2 - int(2)
  Item 3 - int(3)
  int(2)

I am not sure which is best, but it seems to me that any other behaviour
is incorrect.

Actual result:
--------------
In PHP4 (4.4.9):

  int(3)
  Item 1 - int(1)
  Item 2 - int(1)
  Item 3 - int(1)
  int(1)

In PHP5 (5.2.10)

  int(3)
  Item 1 - int(2)
  Item 2 - int(2)
  Item 3 - int(2)
  int(2)

Both of these seem wrong, but the fact that they are different seems
doubly wrong!  Calling reset() before the foreach() loop doesn't make any
difference.  The PHP4 version seems more acceptable (it sets it to the
first element on entry, and then doesn't affect it at all), however the
PHP5 behaviour just seems absurd.

Note: The described behaviour is the same on Windows Vista and Windows XP.

-- 
Edit bug report at http://bugs.php.net/?id=50223&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=50223&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=50223&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=50223&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=50223&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=50223&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=50223&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=50223&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=50223&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=50223&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=50223&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=50223&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=50223&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=50223&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=50223&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=50223&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=50223&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=50223&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=50223&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=50223&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=50223&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=50223&r=mysqlcfg

Reply via email to