The PHP $ _SESSION nested array is lost when the dummy foreach () name matches the array name

Can someone please acknowledge that the following is a bug with PHP 5.2.13: (thanks)

<?php
    session_start();
    if (!is_array($_SESSION["breadcrumb"]["trail"]))
    {
        $_SESSION["breadcrumb"]["trail"][] = "trail";
    } 
    foreach ($_SESSION["breadcrumb"]["trail"] as $breadcrumb)
    {
        echo $breadcrumb;
    }
?>

The aforementioned PHP script will work the third time it is run. It seems that the foreach () loop has a (inappropriate) side effect that wipes out the $ _SESSION nested array, because the internal variable used in the loop matches the name of the nested $ _SESSION array. Just changing the name of the foreach () internal variable to something else fix the problem.

Note: clear session variables before running the script 3 times.

, "$ breadcrumb" "$ the_breadcrumb" . foreach() . : $breadcrumb $_SESSION [ "breadcrumb" ], .

, print_r() , () () " ([ ] = > " ), ( ).

PHP : PHP : 5

PHP 5.3 - PHP 5.2.13. PHP- PHP PHP (php.net), 5.2.13 , , , - . php.net.

,

:

5.2 5.2.2.

:

PHP 5.2.13 5.

+3
3

. notJim register_globals php.ini. ".". Off, , . : register_globals ( , ) PHP 5.3 - , .

+1

, . foreach - , , . php 5.3.0.

+1

I am using 5.2.6 and welcome work without errors.

0
source

Source: https://habr.com/ru/post/1747066/


All Articles