Edit report at http://bugs.php.net/bug.php?id=51907&edit=1
ID: 51907 User updated by: danieln at javanet dot com Reported by: danieln at javanet dot com Summary: $_SESSION nested array is lost when a foreach() dummy name matches array name -Status: Open +Status: Closed Type: Bug Package: Session related Operating System: Linux PHP Version: 5.2.13 New Comment: Solved - Not a bug. Sorry for false alarm. I was not aware of the "register_global" php.ini setting, which merges the scope of local and superglobal arrays. Previous Comments: ------------------------------------------------------------------------ [2010-05-25 19:54:07] danieln at javanet dot com Description: ------------ I'd appreciate confirmation that the following is a bug with PHP 5.2.13, and apologies if I'm mistaken. The following complete PHP script crashes the 3rd time it is run. The foreach() loop seems to have an (improper) side effect which wipes out the nested $_SESSION array because the internal variable used in the loop matches the name of the nested $_SESSION array. Simply changing the name of the internal foreach() variable to something different fixes the problem. Note: clear session variables before running script 3 times. Again, changing "$breadcrumb" to "$the_breadcrumb" fixes the problem. But the foreach() loop should have no side effects. Note: since the scope of $breadcrumb is not the same as the scope of $_SESSION["breadcrumb"], there should be no collision. Note that doing a print_r() on the array shows the array as (correctly) empty the first time, (correctly) populated the second time, and erroneously set as "Array ( [breadcrumb] => trail )" the third time (the nested array has been wiped out). The error in the PHP error log from the 3rd run: PHP Fatal error: Cannot use string offset as an array on line 5 The issue is not a problem on PHP 5.3 - only PHP 5.2.13. I could not find any note regarding this issue in the PHP changelog, and I must use 5.2.13 on my live site, so I'm posting here for confirmation that this is a PHP 5.2 bug. Thanks, Dan Nissenbaum Test script: --------------- <?php session_start(); if (!is_array($_SESSION["breadcrumb"]["trail"])) { $_SESSION["breadcrumb"]["trail"][] = "trail"; } foreach ($_SESSION["breadcrumb"]["trail"] as $breadcrumb) { echo $breadcrumb; } ?> Expected result: ---------------- No PHP 5.2.13 crash on line 5. Actual result: -------------- PHP 5.2.13 crashes on line 5. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=51907&edit=1