php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12533 Reference and global keyword don't work as documented
Submitted: 2001-08-02 10:26 UTC Modified: 2001-12-14 14:37 UTC
From: pmateesc at dynasty dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.0.6 OS: W2k, Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: pmateesc at dynasty dot com
New email:
PHP Version: OS:

 

 [2001-08-02 10:26 UTC] pmateesc at dynasty dot com
<?
$a = 1;
$b = 2;

function Sum() {
	global $a, $b;
	$b = &$a;
}
echo "a=$a b=$b";
?>

I expect echo to output "a=1 b=1" since within Sum I set the variable $b (which refers to global $b) as a reference to $a. 
However, this works using $GLOBALS instead of the keyword global.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-12-14 14:37 UTC] yohgaki@php.net
This is explained in the Manual. Please ask support question to php-general.   
Note: Reference is not a pointer. This is confiuing, but it will not be fixed.
 [2010-11-09 23:13 UTC] magister99 at hotmail dot com
Hi! What is posted in the manual does not work as expected:

class DTO
{
   public $msgs = "ergegefr"; 
   public $a = 1;
   public $b = 2;
   
   function Insert(){ 
      global $msgsb, $a, $b;
      $b = $a + $b;
      	print $msgs; //Prints nothing
      print $b; //Prints 0
   }
}
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed May 07 14:01:30 2025 UTC