Results 1 to 4 of 4

Thread: Arrays in and outside of Functions

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default Arrays in and outside of Functions

    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:

    PHP Code:
    $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();?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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?
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    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.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    alexjewell (05-21-2010)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •