php.net |  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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ssigwart at yahoo dot com
New email:
PHP Version: OS:

 

 [2019-12-24 05:19 UTC] ssigwart at yahoo dot com
Description:
------------
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'];
}
func1();
var_dump($_SESSION['var']);
var_dump($ref);
var_dump($nonRef);

Expected result:
----------------
int(1)
int(1)
int(1)

Actual result:
--------------
int(1)
NULL
int(1)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-24 05:46 UTC] thedinosaurmail at gmail dot com
https://3v4l.org/WGsD9

it seems  same at ervery version
 [2019-12-24 06:49 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-12-24 06:49 UTC] requinix@php.net
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-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 11:01:30 2024 UTC