#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on Apr 7, 2012 13:07:39 GMT -8
Greetings,
I have an issue using the $GLOBALS array in PHP.
<?php $var =array('bar', 'foo'); foreach ($var as $something) { $GLOBALS['var'][] = doSomething($something); } ?>
I need to use the processed version of $var ($GLOBALS['var']) in an external function, however if I reference the processed version in the main file, I have the original array concated with the processed array.
From my understanding, in the external file $GLOBALS['var'] is only set if I declare it, but in the scope of the main file, $GLOBALS['var'] equals $var, even if I don't assign it.
Is there a way to add needed variables to a globally accessible variable, but only if I do so manually, or would I be better to just create my own global system like below?
<?php $var =array('bar', 'foo'); foreach ($var as $something) { $_myglobalvar['var'][] = doSomething($something); }
function foobar() { global $_myglobalvar; } ?>
- RedBassett
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Apr 7, 2012 13:57:08 GMT -8
If you still need $var to be equal to Array('bar', 'foo'), then you need a separate variable name. One variable can't equal two values.
If you don't need bar and foo, then do: foreach ($var as $key => $value) $var[$key] = doSoemthing($value);
Then access $var from within a function via global $var;
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on Apr 7, 2012 20:48:00 GMT -8
If you still need $var to be equal to Array('bar', 'foo'), then you need a separate variable name. One variable can't equal two values. If you don't need bar and foo, then do: foreach ($var as $key => $value) $var[$key] = doSoemthing($value); Then access $var from within a function via global $var; Essentially what I posted in the second part, right? I converted the code to that format on a different git branch, and it seems to be working fine. - RedBassett
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Apr 7, 2012 21:29:56 GMT -8
Sure. There is essentially need to ever use $GLOBALS that I can think of. global $the_var_name does the job, and it rids the confusion you seem to be running into that $GLOBALS['var'] and $var are somehow different (or that appending data to the $GLOBALS['var'] array doesn't also append it to $var).
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on Apr 7, 2012 21:53:04 GMT -8
Sure. There is essentially need to ever use $GLOBALS that I can think of. global $the_var_name does the job, and it rids the confusion you seem to be running into that $GLOBALS['var'] and $var are somehow different (or that appending data to the $GLOBALS['var'] array doesn't also append it to $var). The reason I didn't want to use the keyword is because I have multiple global variables, such as my DB connection, my user's data, etc. I realized I could solve this by do $_customGlobalVar['conn'] and $_customGlobalVar['user'], instead of $GLOBALS['conn'] and $GLOBALS['user']. What confused me about $GLOBALS is the fact that it is different depending on where you call it. Thanks for the input!
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Apr 9, 2012 10:42:13 GMT -8
You can make multiple variables global.
global $conn, $user;
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on Apr 9, 2012 11:15:23 GMT -8
You can make multiple variables global. global $conn, $user; Yes, but I wanted to avoid this, hence why I was trying to use the $GLOBALS before. Especially in this case, I have easily ten variables needed in a large number of functions. Too much copy/paste or typing, and then I'd have to update them all if the structure changed. - RedBassett
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Apr 9, 2012 13:10:49 GMT -8
Then why not just use $GLOBALS['var'] within the function, without porting them all to a separate variable?
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on Apr 9, 2012 19:18:21 GMT -8
Then why not just use $GLOBALS['var'] within the function, without porting them all to a separate variable? The $GLOBALS changes depending on the scope it seems, and it was just causing scope issues where a line of code would work differently in different locations. While one more variable, having a custom global var allows me a lot more control over what the code is doing. Are there major disadvantages (speed, memory, etc) to a custom variable, or or just the writing of a different name? - RedBassett
|
|
inherit
40157
tyrantlytamale 627939549 tjhtmlmaniac
0
Sept 3, 2023 15:17:02 GMT -8
Tylr
The stale taste of recycled air.
2,964
April 2005
tyrantlytamale
|
Post by Tylr on Apr 10, 2012 6:03:32 GMT -8
The $GLOBALS changes depending on the scope it seems, and it was just causing scope issues where a line of code would work differently in different locations. While one more variable, having a custom global var allows me a lot more control over what the code is doing. It sounds like you answered your own question, mate. I don't see a any major disadvantages to using your own custom global variable.
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Apr 10, 2012 12:24:49 GMT -8
Slightly more RAM use, but that's such a level of micro-optimization you don't need to be worried about at all.
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on Apr 10, 2012 15:05:30 GMT -8
The $GLOBALS changes depending on the scope it seems, and it was just causing scope issues where a line of code would work differently in different locations. While one more variable, having a custom global var allows me a lot more control over what the code is doing. It sounds like you answered your own question, mate. I don't see a any major disadvantages to using your own custom global variable. Yeah, in the process of the thread, I started figuring out how $GLOBALS was working. Slightly more RAM use, but that's such a level of micro-optimization you don't need to be worried about at all. Not ready to micro optimize yet Thanks! - RedBassett
|
|