php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #45369 $_SESSION-containing-arrays assignment to var does not correspond to manual
Submitted: 2008-06-26 15:31 UTC Modified: 2008-11-05 10:54 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: thomas dot meike at nic dot at Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.6 OS: *
Private report: No CVE-ID: None
 [2008-06-26 15:31 UTC] thomas dot meike at nic dot at
Description:
------------
you define an array
step through ONLY SOME elements using list-each structure
stepping through you perform some changes on data
finishing the while-structure the array is saved in session
let's say, somewhere else in your businesslogic you need these session data once more:
you initiate a variable with the recently saved session array 
the PROBLEM now: the variable is initiated with the array NOT RESETTED
as defined in documentation an array assigned to a variable has to be resetted
the problem exists also with php version 5.2.5


Reproduce code:
---------------
session_start();		
$arrL = array("test1","test2","test3","test4","test5","test6","test7","test8","test9","test10");
if(!isset($_SESSION['DATA'])){
	$_SESSION['DATA']=$arrL;
}
			$counter = 3;
			$arrSessData = $_SESSION['DATA'];
			while((list($index, $value) = each($arrSessData)) && ($counter > 0) ) {
				$arrSessData[$index] =  '_test_' . $value;
				$counter--;
			}
			//store changes in session
			$_SESSION['DATA']=$arrSessData;
			//somewhere else within the businesslogic, init local variable and step through
			$arrSessDataProc = $_SESSION['DATA'];
			while((list($index, $value) = each($arrSessDataProc)) ){
				echo "<br>" . $index . ' ' . $value;
			}


Expected result:
----------------
0 _test_test1
1 _test_test2
2 _test_test3
3 test4
4 test5
5 test6
6 test7
7 test8
8 test9
9 test10

Actual result:
--------------
4 test5
5 test6
6 test7
7 test8
8 test9
9 test10

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-01 09:49 UTC] jani@php.net
You should reset() it "manually", I don't see any wrong behaviour here. Where in the manual does it say that assiging to the "magic" $_SESSION superglobal the array is reset automatically??
 [2008-07-01 11:05 UTC] thomas dot meike at nic dot at
>>You should reset() it "manually", I don't see any wrong behaviour >>here.

yes, resetting it manually is always possible

>>Where in the manual does it say that assiging to the "magic" 
>>$_SESSION
>>superglobal the array is reset automatically??

I did not talk abount assignments to $_SESSION!!!

the only thing i wanted to point out here is that now, assigning $_SESSION-containing array-constructs to any local variable does not work  any more as it did before or lets say it does not correspond to the following part in the manual: http://www.php.net/manual/en/language.types.array.php

Array assignment always involves value copying. It also means that the internal array pointer used by current() and similar functions is reset. Use the reference operator to copy an array by reference.
 [2008-07-02 09:31 UTC] jani@php.net
Always a simple test is better than some excerpt from a huge app:

# php -r '$a = array(1,2,3); next($a); $b = $a; var_dump(current($b));'
int(2)

So it seems the manual is wrong again.
 [2008-11-05 10:54 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

I guess that the behavior changed in some historic version. So the documentation was originally correct.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 16:01:36 2025 UTC