php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68595 variable variables scope problem in for loops
Submitted: 2014-12-12 15:03 UTC Modified: 2015-01-04 04:22 UTC
From: jmichae3 at yahoo dot com Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.5.19 OS: windows 7 sp1 x64
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jmichae3 at yahoo dot com
New email:
PHP Version: OS:

 

 [2014-12-12 15:03 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-12 15:20 UTC] 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 19:20 UTC] aharvey@php.net
-Status: Open +Status: Feedback
 [2014-12-12 19:20 UTC] aharvey@php.net
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.
 [2014-12-13 16:02 UTC] jmichae3 at yahoo dot com
that *is* reduced. the source program is large and has several related files. I knew someone was going to say that, so I reduced it, but I didn't think someone was going to ask for further reduction. sorry, can't be done.

the for statement's {} apparently DO have scope. once I put the global statement in {} it doesn't work.
 [2014-12-23 19:06 UTC] ab@php.net
@jmichae3 at yahoo dot com, you can do that. Leave only one variable, remove the strtoupper() loop, remove peak usage check, etc. Please leave only what is relevant to the matter you report.

Thanks.
 [2015-01-04 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC