php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35267 Foreach value overwritten
Submitted: 2005-11-17 18:19 UTC Modified: 2005-11-17 18:57 UTC
From: mdunn at escindex dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.0.5 OS: FreeBSD 4.10
Private report: No CVE-ID: None
 [2005-11-17 18:19 UTC] mdunn at escindex dot com
Description:
------------
According to the documentation "foreach" operates on a copy of the specified array and not the array itself.  I believe that in PHP5 something has been messed up as it works in PHP4.  There was a logic error in our script (missing double ==), however in PHP4 it had no effect on the outcome.  In PHP5 it actually changed the value.

Screen shots available at http://ww1.escindex.com/phpbug/

Reproduce code:
---------------
$items['product_type'] = 'CPKG';
$items['product_id'] = 2;

foreach($items as $item)
{
  if($item->product_type == 'CPKG' && ($item->product_id = 1 || $item->product_id == 2))
  {

Expected result:
----------------
$items['product_type'] = 'CPKG';
$items['product_id'] = 2;

Actual result:
--------------
$items['product_type'] = 'CPKG';
$items['product_id'] = 1;

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-11-17 18:35 UTC] tony2001@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

$item->product_id = 1 <-
 [2005-11-17 18:57 UTC] mdunn at escindex dot com
Unless you are referring to the -> "referencing" a variable, it is a bug.  We have 2 "live" servers. On server 1 (PHP4), the script runs (even with the missing =) and DOES NOT change the value.  On server 2 (PHP5) the script runs and DOES change the value.  Once we add in the second =, both scripts run as expected.  So our logic error compounded with the addition of PHP5 overwrites the value.  No where in the documentation does it mention this for PHP4 or PHP5.  Even if it wasn't a logic error, and we really wanted to set the "copy" of the value to 1, it SHOULD NOT change the master value.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 21:01:30 2024 UTC