Bug #68595 [Opn->Fbk]: variable variables scope problem in for loops
| From: | aharvey@php.net | Date: | Fri, 12 Dec 2014 19:20:46 +0000 |
| Subject: | Bug #68595 [Opn->Fbk]: variable variables scope problem in for loops | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189046@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68595&edit=1
ID: 68595
Updated by: aharvey@php.net
Reported by: jmichae3 at yahoo dot com
Summary: variable variables scope problem in for loops
-Status: Open
+Status: Feedback
Type: Bug
Package: Scripting Engine problem
Operating System: windows 7 sp1 x64
PHP Version: 5.5.19
Block user comment: N
Private report: N
New Comment:
What do you mean by "doesn't work"? Can you reduce this to a minimal, runnable
example, please?
Note that PHP doesn't have block scope, so any loops are within the same scope as the function
as a whole.
Previous Comments:
------------------------------------------------------------------------
[2014-12-12 15:20:58] jmichae3 at yahoo dot com
workaround is to avoid curly braces in th efor loops altogether and nest for loops and use 2 source
arrays:
http://pastebin.com/RMuqU3AF
------------------------------------------------------------------------
[2014-12-12 15:03:58] jmichae3 at yahoo dot com
Description:
------------
global keyword doesn't work in for loops because of scope issues I guess. causes a problem with
my code, which wants to generate a collection of variable variables. I must manual,ly declare them
all rather than iterating through the loop. the variable variables generated for stats are:
$VISITORfilecount
$VISITORfoldercount
$VISITORhttpcount
$ADMINfilecount
$ADMINfoldercount
$ADMINhttpcount
$PAIDWEBAPPSfilecount
$PAIDWEBAPPSfoldercount
$PAIDWEBAPPShttpcount
...
you COULD generate them using a nested for loop, which might be next. theoretically. but it
doesn't work practically. because of scope problems, code fails in the Report() function and
variable is invisible unless you have the line
global ${'VISITORfilecount'};
in the code (as in the example), normally this would not be in my code, because the list of
VISITOR... changes with the web sites I configure for in the config file in which this resides -
manually globalling is unnecessary.
Test script:
---------------
http://pastebin.com/vbr3TAq6
Expected result:
----------------
December 12, 2014
ADMINfoldercount=0
ADMINhttpcount=0
ADMINfilecount=0
PAIDBLSfoldercount=0
PAIDBLShttpcount=0
PAIDBLSfilecount=0
PAIDMUSICfoldercount=0
PAIDMUSIChttpcount=0
PAIDMUSICfilecount=0
PAIDWEBAPPSfoldercount=0
PAIDWEBAPPShttpcount=0
PAIDWEBAPPSfilecount=0
VISITORfoldercount=0
VISITORhttpcount=0
VISITORfilecount=1
skipped file count:
total file count: 1
total folder count: 0
total count including http: 1
Peak Ram Usage: 364488
Total Filesize in menu tree: 0
RAM/Filesize ratio:20.089731576917
Actual result:
--------------
without the manual global line:
December 12, 2014
ADMINfoldercount=0
ADMINhttpcount=0
ADMINfilecount=0
PAIDBLSfoldercount=0
PAIDBLShttpcount=0
PAIDBLSfilecount=0
PAIDMUSICfoldercount=0
PAIDMUSIChttpcount=0
PAIDMUSICfilecount=0
PAIDWEBAPPSfoldercount=0
PAIDWEBAPPShttpcount=0
PAIDWEBAPPSfilecount=0
VISITORfoldercount=0
VISITORhttpcount=0
VISITORfilecount=0
skipped file count:
total file count: 0
total folder count: 0
total count including http: 0
Peak Ram Usage: 364488
Total Filesize in menu tree: 0
RAM/Filesize ratio:20.089731576917
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68595&edit=1