php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63714 incorrect global statement
Submitted: 2012-12-07 02:37 UTC Modified: 2012-12-08 02:51 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: axdr at bk dot ru Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.4.9 OS: Windows
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: axdr at bk dot ru
New email:
PHP Version: OS:

 

 [2012-12-07 02:37 UTC] axdr at bk dot ru
Description:
------------
'global'-declaration inside a function dosn't affect referencies.

Test script:
---------------
function func() {
    global $a, $b;
    $a = 'aaa';
    $b = &$a;
    $GLOBALS['c'] = &$a;
}
func();
echo "$a, $b, $c"; 



Expected result:
----------------
aaa, aaa, aaa

Actual result:
--------------
aaa, , aaa


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-07 02:44 UTC] axdr at bk dot ru
'global'-declaration kills referencies

  function func() {
    global $a, $b;
    $a = 'aaa';
    $b = &$a;
    echo "$a, $b", '<br>';
    global $a, $b;
    echo "$a, $b";
  }
  func();

output: 
  aaa, aaa
  aaa,
 [2012-12-07 02:52 UTC] laruence@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

global $a;
  make a local variable 'a' reference to $GLOBALS['a'];

also thinking of:

$b = &$a;  // b reference to a
$b = &$c;  // change reference to c


then:
  global $a, $b;   //b reference to $GLOBALS['b']
  $a = 'aaa';
  $b = &$a;        //b now reference to local $a with GLOBALS['b'] unchanged
 [2012-12-07 02:52 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2012-12-07 02:54 UTC] laruence@php.net
also see:

<?php
function func() {
    global $a, $b;
    $a = 'aaa'; 
    $b = &$a;
    $b = 'bbb';
}   
    
func();
var_dump($a);

output:
bbb
 [2012-12-07 04:08 UTC] axdr at bk dot ru
Sorry. You are right.
But I think this is irrational. 
It's impossible to read all documentation and to remember word for word.
This is a kind of underwater mine.
 [2012-12-08 00:00 UTC] axdr at bk dot ru
global $var is reference to $GLOBALS['var']
but is not equivalent to $var = &$GLOBALS['var']

$gvar = 'aaa';
$var = 'bbb';
function func2() {
  global $gvar;
  global $var;
  $GLOBALS['var'] = &$gvar;
  var_dump($var, $GLOBALS['var']);
}
function func1() {
  global $gvar;
  $var = &$GLOBALS['var'];
  $GLOBALS['var'] = &$gvar;
  var_dump($var, $GLOBALS['var']);
}
function func3() {
  $gvar = 'aaa';
  $var1 = 'bbb';  // вместо global $var
  $var = &$var1;
  $var1 = &$gvar;
  var_dump($var, $var1);
}
func1();
echo '<hr>';
func2();
echo '<hr>';
func3();

output:
'bbb'
'aaa'
---------
'aaa'
'aaa'
---------
'bbb'
'aaa'
 [2012-12-08 00:17 UTC] axdr at bk dot ru
Please, ignore previous comment

function func1() {
  global $gvar;
  $var = &$GLOBALS['var'];
  $GLOBALS['var'] = &$gvar;
  var_dump($var, $GLOBALS['var'], $gvar);
}
function func2() {
  global $gvar;
  global $var;
  $GLOBALS['var'] = &$gvar;
  var_dump($var, $GLOBALS['var'], $gvar);
}

$gvar = 'aaa';
$var = 'bbb';
func1();
echo '<hr>';
$gvar = 'aaa';
$var = 'bbb';
func2();

output: 

string 'bbb'
string 'aaa'
string 'aaa'
--------------------------------------------------------------------------------
string 'bbb'
string 'bbb'
string 'bbb'
 [2012-12-08 02:51 UTC] laruence@php.net
try this:

$gvar = 'aaa';
$var = 'bbb';
func1();

unset($gvar);   //reset the environment
unset($var);

$gvar = 'aaa';
$var = 'bbb';
func2();
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 18:01:32 2025 UTC