Bug #71988 [Opn->Nab]: an element of $GLOBALS gets inexplicably rewritten

From: Date: Fri, 08 Apr 2016 12:37:27 +0000
Subject: Bug #71988 [Opn->Nab]: an element of $GLOBALS gets inexplicably rewritten
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200437@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71988&edit=1 ID: 71988 Updated by: requinix@php.net Reported by: liv999 at hotmail dot com Summary: an element of $GLOBALS gets inexplicably rewritten -Status: Open +Status: Not a bug Type: Bug Package: *General Issues Operating System: Linux/CentOS, Ubuntu PHP Version: Irrelevant Block user comment: N Private report: N New Comment: $_GLOBALS isn't just another array. It's automagically the variables in the global scope. Your foreach will overwrite the values because it used the variable "$categories". http://php.net/manual/en/reserved.variables.globals.php Use a different variable. https://3v4l.org/s7T2o Previous Comments: ------------------------------------------------------------------------ [2016-04-08 12:22:36] liv999 at hotmail dot com Description: ------------ I encountered a strange behavior of $GLOBALS array. After defining an element of it, then using another element of it in some initializations and calculations, the iniital element gets affected with values from the operations performed, although it was not involved. If I change the GLOBALS name to something else (like GLOBALS2) the result is correct, thus it has to do with the GLOBALS properties. Test script: --------------- <?php //initialization of an element of GLOBALS $GLOBALS['categories'] = array(1=>'z',2=>'x',3=>'c',4=>'v',5=>'r'); print_r($GLOBALS['categories']); //another element $GLOBALS['videos'] = array(1=>'a',2=>'b',3=>'c',4=>'d'); //some operations $videosCategories = array(1=>array(2,3,4,5),2=>array(12,13,14,15),3=>array(22,23,24,25),4=>array(32,33,34,35)); foreach($videosCategories as $videoId =>$categories) $GLOBALS['videos'][$videoId]['category'] = implode(',',$categories); //the initial element is reqritten print_r($GLOBALS['categories']); Expected result: ---------------- The 2 arrays should be the same, as it is the same GLOBALS element. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71988&edit=1

« previous php.bugs (#200437) next »