php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42932 reference operator is different between $GLOBALS array and declared global
Submitted: 2007-10-11 17:42 UTC Modified: 2007-10-12 09:52 UTC
From: cpu-g at yahoo dot com dot tw Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.4 OS: win xp
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: cpu-g at yahoo dot com dot tw
New email:
PHP Version: OS:

 

 [2007-10-11 17:42 UTC] cpu-g at yahoo dot com dot tw
Description:
------------
I use reference operator in local variable calculatedly. Because I
know that reference is similar to pointer in C language and they
are dangerous in some situation,I want to know that if PHP allow 
reference to local variable.

But $GLOBALS array allowed reference to local variable,
declared global keyword failed.



Reproduce code:
---------------
<?php
$global1=100;
$global2=100;
function foo() 
{ 
    $local=10; 
    global $global1;
    $global1=&$local;
    $GLOBALS["global2"] =&$local;
} 

foo();
echo "$global1 : $global2 <BR>"; 
?>

Expected result:
----------------
100 : 100   or   10 : 10.

1st output is good for any situation. It guarantee that reference to the local variable is not allowed. So Reference operator can't work in 2 style: $GLOBALS array and declared global. but actually
only $GLOBALS array can work.

2st output is bad. It symbolize that we still can access local
variable in function foo() after calling foo(). It may result in
access violation.  

Actual result:
--------------
100 : 10 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-12 09:52 UTC] jani@php.net
I guess it's your misunderstanding how references work.
First of all, they're NOT like pointers:

http://www.php.net/manual/en/language.references.arent.php

See more about references here:

http://www.php.net/manual/en/language.references.php
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Dec 30 11:00:01 2025 UTC