View Full Version : Arrays in and outside of Functions
alexjewell
05-21-2010, 02:57 PM
I have an array defined outside of a function. Both are inside of a file I include other places, and I use filterFunc(); on those other pages. In order for the function to run through the array, I need to define the array inside of it, as well. Unfortunately, I need to use the array elsewhere and don't feel like going through the globalizing thing. Here's what I have:
$filterArr = array(
"all" => "All",
"sn" => "S/N",
"tags" => "Tags",
"vendor" => "Vendor",
"purdate" => "Pur Date",
"ticketnum" => "Ticket Num");
function filterFunc() {
$filterArr = array(
"all" => "All",
"sn" => "S/N",
"tags" => "Tags",
"vendor" => "Vendor",
"purdate" => "Pur Date",
"ticketnum" => "Ticket Num");
foreach($filterArr as $key => $val){
echo '<option value="'.$key.'">'.$val.'</option>'."\n";
}
}
Is there a reason why I can't just use $filterArr without redefining it inside of filterFunc();?
djr33
05-21-2010, 04:44 PM
I ... don't feel like going through the globalizing thing.
Your choice, but that's the exact method for doing this. If not, how do you expect it to be connected?
Add: global $array; as the first line of your function. What's so hard about that?
Overusing globals isn't a great idea (mostly because it gets messy), but this is a perfect example of a time you should be using it.
There are only two other ways I can think of to do this:
1. Feed this to the function as an argument. But that's extra work in this case, since it's standard in the function.
2. Store the array in an external file; include it into both the global (main) scope and within the function. But... why?
alexjewell
05-21-2010, 06:00 PM
Hey, I thought globalizing was more difficult than that, but after some google searching, discovered that global $var; was all I had to do. Fixed. lol. Thanks, bro.
djr33
05-21-2010, 07:35 PM
There are two kinds of global variables: "superglobals" that exist everywhere and "globals" that, well, don't.
"superglobals" are a closed set of special variables setup by the system including $_POST, $_SYSTEM, etc. Mostly config/user data stuff. You cannot make your own (unfortunately), as far as I know.
"globals" are defined as anything operating in the highest scope (outside of all functions). ANY variable there is by default a global. It is also "local" in the main scope. Any variable defined within a function [or class] is "local". If you want to attach it to the variable of the same name outside the function, just use the keyword 'global', and that's it.
Note also that the keyword "global" is ONLY for that particular function and for any other functions to do the same thing you'll need to use global in there too.
Note also that includes DO not change scope: it's just like putting the code into the current page, so scope remains the same as if it were in the same page, whether in a function or not.
A lot of the descriptions of this are confusing, but that's the basic info: variables exist only within the scope they are created, with the single exception of variables you call "global" in any given function, where then it uses the highest level variable.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.