PHP 4.0 Bug #6161 Updated: Global variable acts strangely

From: Date: Wed, 29 Nov 2000 11:29:37 +0000
Subject: PHP 4.0 Bug #6161 Updated: Global variable acts strangely
Groups: php.dev 
Request: Send a blank email to php-dev+get-39509@lists.php.net to get a copy of this message
ID: 6161 Updated by: sniper Reported By: andrew@andrew.org Status: Bogus Bug Type: *General Issues Assigned To: Comments: Previous Comments: --------------------------------------------------------------------------- [2000-08-15 09:22:22] waldschrott@php.net Xou´re right, that seems to be a bug, the manual says: "If both track_vars and register_globals are enabled, then the globals variables and the $HTTP_SESSION_VARS entries will reference the same value." Thus a variable made global a before session_start()´s should be update accordingly, anyway I´ll propose you to set "register_global" off and access all variables directly throught $HTTP_*_VARS that´ll protect you from such shortcomings but will force you to be more disciplined acting with variables... --------------------------------------------------------------------------- [2000-08-15 06:50:02] andrew@andrew.org Looks like I got away with fantasy. Anyway, I think I cornered the little bastard. look at "globtest2.phtml" : <? function increment_0() { global $a; session_start(); $a += 1; } function increment_1() { session_start(); global $a; $a += 1; } function increment_2() { session_start(); $a += 1; $GLOBALS["a"] += 1; } switch ($b) { case 0: increment_0(); break; case 1: increment_1(); break; case 2: increment_2(); break; } if ($a == 1) session_register("a"); echo $a, "<BR>n"; echo "<A HREF='./globtest2.phtml?b=0'>increment_0()</A><BR>n"; echo "<A HREF='./globtest2.phtml?b=1'>increment_1()</A><BR>n"; echo "<A HREF='./globtest2.phtml?b=2'>increment_2()</A><BR>n"; ?> if you do session_start() AFTER declaring a variable as global (as in increment_0() ), you lose the changes. I don't think this is how it's supposed to be. In the above examples, the first link isn't working properly. (BTW, remember to quit your browser to unload the session cookie) what about this? Luca (going to sleep now...) --------------------------------------------------------------------------- [2000-08-15 05:55:51] andrew@andrew.org I have some code whose behaviour changes unexpectedly (at least as far as I'm concerned :-). I am modifying some global var value inside a function and reading this value in some class method accessing it defining it "global", and it does different things whether I declare the variable as global or access it through the $GLOBALS array, in the modifying function. I don't know if this is not meant to be, I thought the two syntaxes had the same use, and nothing else makes me doubt about this (not even the manual). It took me hours to isolate the problem. It looks rather nasty. The suspect code involves sessions (the global var in subject is registered), that's why I marked this report as session related. It looks as if the changes don't get applied to the session if I change the variable's value when defined as global in my function, while they do get applied when I use the $GLOBALS array. But I have not been able to reproduce this (alleged) misbehaviour on purpose. Please let me know. --------------------------------------------------------------------------- Full Bug description available at: http://bugs.php.net/?id=6161

« previous php.dev (#39509) next »