php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #4552 references inside functions work with GLOBALS, not global
Submitted: 2000-05-22 20:23 UTC Modified: 2000-05-30 19:45 UTC
From: bill at rsv dot ricoh dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0 Release Candidate 2 OS: RH Linux 2.2.12-20
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: bill at rsv dot ricoh dot com
New email:
PHP Version: OS:

 

 [2000-05-22 20:23 UTC] bill at rsv dot ricoh dot com
 // Bug-script for assigning references for global variables inside functions
  $fruit = "apple";
  $rock = "granite";

  function twiddle(){
    global $fruit, $rock;
    $fruit = &$rock;

    $GLOBALS["rock"] = &$GLOBALS["fruit"]; 

    // the "global" command works here
    echo "inside : fruit=$fruit rock=$rock<br>\n";

  }

  echo "BEFORE: fruit=$fruit  rock=$rock<br>";
  twiddle();
  echo "AFTER: fruit=$fruit  rock=$rock";
  // But only the reassignment using GLOBALS has an effect outside
  // the function; the "global" assignment  persists only inside.

----------------
PHP configured with mysql ; otherwise plain vanilla.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-05-30 19:45 UTC] stas at cvs dot php dot net
This is not bug. It goes as follows: 
1. "global" means local ones become pointers to global ones
2. local fruit becomes pointer to the same as local rock 
(which is "granite")
3. global rock becomes pointer to the same as global fruit
(which is "apple")
 but local rock (and thus fruit) still point to "granite"
4. Now when function exits, local $fruit and $rock are destroyed,
and both $fruit and $rock point to "apple"

Think about it as if all variables in PHP are pointers to some entities.
Ask on the list if you need more guidance.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Sep 28 19:01:26 2024 UTC