|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79025 Assigning superglobals by reference in functions are reset to null
Submitted: 2019-12-24 05:19 UTC Modified: 2019-12-24 13:57 UTC
From: ssigwart at yahoo dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.4.1 OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: ssigwart at yahoo dot com
New email:
PHP Version: OS:


 [2019-12-24 05:19 UTC] ssigwart at yahoo dot com
When you assign a variable to a superglobal ($_SESSION, $_GET, etc) by reference within a function, the variable is reset to null once you exit the function.

Test script:
$_SESSION['var'] = 1;
function func1()
	global $ref;
	global $nonRef;
	$ref = &$_SESSION['var'];
	$nonRef = $_SESSION['var'];

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-24 05:46 UTC] thedinosaurmail at gmail dot com

it seems  same at ervery version
 [2019-12-24 06:49 UTC]
-Status: Open +Status: Not a bug
 [2019-12-24 06:49 UTC]
When you
  global $ref;
PHP created a local variable inside the function named "ref" which acts as a reference to the global variable outside the function also named "ref". Since it didn't exist yet, it was given the value of null.

When you
  $ref = &$_SESSION['var'];
you severed the connection between the local and global $refs and instead gave the local $ref a new reference to $_SESSION['var'].

The global $ref started with a value of NULL and never changed from that.
 [2019-12-24 13:57 UTC] ssigwart at yahoo dot com
Thanks for the quick response and details.  That makes sense.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sun Oct 01 03:01:25 2023 UTC