#42932 [Opn]: reference operator is different between $GLOBALS array and declared global

From: Date: Fri, 12 Oct 2007 08:30:45 +0000
Subject: #42932 [Opn]: reference operator is different between $GLOBALS array and declared global
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-118447@lists.php.net to get a copy of this message
ID: 42932 User updated by: cpu-g at yahoo dot com dot tw Reported By: cpu-g at yahoo dot com dot tw Status: Open -Bug Type: Apache2 related +Bug Type: Performance problem Operating System: win xp PHP Version: 5.2.4 New Comment: I corrected the Category. Previous Comments: ------------------------------------------------------------------------ [2007-10-11 17:42:58] cpu-g at yahoo dot com dot tw Description: ------------ I use reference operator in local variable calculatedly. Because I know that reference is similar to pointer in C language and they are dangerous in some situation,I want to know that if PHP allow reference to local variable. But $GLOBALS array allowed reference to local variable, declared global keyword failed. Reproduce code: --------------- <?php $global1=100; $global2=100; function foo() { $local=10; global $global1; $global1=&$local; $GLOBALS["global2"] =&$local; } foo(); echo "$global1 : $global2 <BR>"; ?> Expected result: ---------------- 100 : 100 or 10 : 10. 1st output is good for any situation. It guarantee that reference to the local variable is not allowed. So Reference operator can't work in 2 style: $GLOBALS array and declared global. but actually only $GLOBALS array can work. 2st output is bad. It symbolize that we still can access local variable in function foo() after calling foo(). It may result in access violation. Actual result: -------------- 100 : 10 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=42932&edit=1

« previous php.bugs (#118447) next »