Bug #75184 [Opn->Nab]: A $GLOBAL variable is magically modified

From: Date: Mon, 11 Sep 2017 10:08:25 +0000
Subject: Bug #75184 [Opn->Nab]: A $GLOBAL variable is magically modified
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-211060@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75184&edit=1 ID: 75184 Updated by: kelunik@php.net Reported by: hdfou at yahoo dot fr Summary: A $GLOBAL variable is magically modified -Status: Open +Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: windows 10 PHP Version: 7.0.23 Block user comment: N Private report: N New Comment: You're in the global scope with your "foreach". foreach( $GLOBALS['availableLanguages'] as $lang => $value){ That modifies the global $lang. I'd recommend to not use $GLOBALS. Previous Comments: ------------------------------------------------------------------------ [2017-09-11 09:57:02] hdfou at yahoo dot fr Description: ------------ A $GLOBALS value is updated and should not be updated. Test script: --------------- $GLOBALS['availableLanguages'] = array( 'fr' => array( 'n' => 'Français' ) , 'en' => array( 'n' => 'English' ) ); $GLOBALS['lang']='fr'; echo 'At the biginning, $GLOBALS[\'lang\']=' . $GLOBALS['lang'] . '<br />' ; foreach( $GLOBALS['availableLanguages'] as $lang => $value){ echo '$lang=' . $lang . '<br />'; } // nowhere $GLOBALS['lang'] has been changed by the script but at the end // $GLOBALS['lang'] = "en" instead of "fr". echo '<br />$GLOBALS[\'lang\']="' . $GLOBALS['lang'] . '" ' . ($GLOBALS['lang']=='en' ? '<b style="color:red;">THIS SHOULD BE "fr"</b>' : '<b style="color:green;">OK</b>' ) ; exit(); ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75184&edit=1

« previous php.bugs (#211060) next »