|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2007-10-12 09:52 UTC] jani@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 30 11:00:01 2025 UTC |
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