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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
28 - 27 = ?
Subscribe to this entry?

 
 [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-2022 The PHP Group
All rights reserved.
Last updated: Wed Oct 05 15:05:52 2022 UTC