Bug #79025 [Nab]: Assigning superglobals by reference in functions are reset to null
| From: | ssigwart at yahoo dot com | Date: | Tue, 24 Dec 2019 13:57:16 +0000 |
| Subject: | Bug #79025 [Nab]: Assigning superglobals by reference in functions are reset to null | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-224519@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79025&edit=1
ID: 79025
User updated by: ssigwart at yahoo dot com
Reported by: ssigwart at yahoo dot com
Summary: Assigning superglobals by reference in functions are
reset to null
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Any
PHP Version: 7.4.1
Block user comment: N
Private report: N
New Comment:
Thanks for the quick response and details. That makes sense.
Previous Comments:
------------------------------------------------------------------------
[2019-12-24 06:49:20] 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 05:46:40] thedinosaurmail at gmail dot com
https://3v4l.org/WGsD9
it seems same at ervery version
------------------------------------------------------------------------
[2019-12-24 05:19:33] 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)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79025&edit=1