Bug #75184 [Com]: A $GLOBAL variable is magically modified
| From: | spam2 at rhsoft dot net | Date: | Mon, 11 Sep 2017 10:15:24 +0000 |
| Subject: | Bug #75184 [Com]: A $GLOBAL variable is magically modified | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-211063@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
Comment by: spam2 at rhsoft dot net
Reported by: hdfou at yahoo dot fr
Summary: A $GLOBAL variable is magically modified
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:
i second not to use $GLOBALS at all, especially in code whcih runs in the global scope because
it's just a useless additional OPCODE and in many cases harder for a IDE to do sane
autocompletion
within function just do "global $varname" which is one OPCODE instead one for each usage
and so no difference if it's used only once and faster when used multiple times
in the sample of the bugreporter $GLOBALS can be completly removed and then you see $lang is used
before and within the loop and it's only not obvious because of the inconsistent usage auf
$GLOBALS['lang'] and $lang which are both the same var in the same scope
Previous Comments:
------------------------------------------------------------------------
[2017-09-11 10:08:23] kelunik@php.net
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.
------------------------------------------------------------------------
[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