php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71988 an element of $GLOBALS gets inexplicably rewritten
Submitted: 2016-04-08 12:22 UTC Modified: 2016-04-08 12:37 UTC
From: liv999 at hotmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: Linux/CentOS, Ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: liv999 at hotmail dot com
New email:
PHP Version: OS:

 

 [2016-04-08 12:22 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-04-08 12:37 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-04-08 12:37 UTC] requinix@php.net
$_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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 19:01:30 2024 UTC