Results 1 to 5 of 5

Thread: Arrays in and outside of Functions

  1. #1
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arrays in and outside of Functions

    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();?

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

    Default

    Add this line to the top of your function:
    global $filterArr;


    If you really "don't feel like globalizing it", then you will need to do what you're doing.
    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
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    !!!!!!!!!!!!!!!!!!!!!
    Last edited by djr33; 07-19-2010 at 01:31 AM.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by alex weber View Post
    !!!!!!!!!!!!!!!!!!!!!
    I'm not sure what you mean by that, but if you want to know why you "can't just use it," then I'll tell you: PHP is designed like that on purpose. Variables (arrays, functions, etc.) in one scope (say, outside a function) are invisible to those in another (say, inside that function). As frustrating as that may be at times, it's in place for a good reason: it prevents accidental conflicts (like accidentally overwriting an important variable when executing an outside function that happens to have a variable with the same name). If you want to know more, read here.

    It's too bad you "don't feel like going through the globalizing thing," because that's basically the only way to get around redefining the variable inside your function (or passing it in through the function call).

    But it's not that hard.

    As djr said,
    PHP Code:
    <?php

    $filterArr 
    = array(
        
    "all" => "All",
        
    "sn" => "S/N",
        
    "tags" => "Tags",
        
    "vendor" => "Vendor",
        
    "purdate" => "Pur Date",
        
    "ticketnum" => "Ticket Num"
        
    );

    function 
    filterFunc() {
       
    // this is basically Example #1 from the php.net link I gave above
       
    global $filterArr;
       foreach(
    $filterArr as $key => $val){
           echo 
    '<option value="'.$key.'">'.$val.'</option>'."\n";
       }
    }

    ?>
    Edit:

    also, you seem to have a bunch of random hyperlinks in your last post. What gives?


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

    Default

    1. I removed a spam reply and banned the user who is now also the original poster, so I will additionally close this discussion.

    2. One note to add to this slightly outdated discussion, for anyone else who might have a similar question is:
    $var = $GLOBALS['var'];
    That is another way to access the value of a global variable from within a function. Alternatively you can use/modify $GLOBALS['var'] directly.
    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

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
  •