0

Possible Duplicate:
PHP global variable not found in functions

I have a function that reads data from a database. I have an array that I want to put data from the database into for easier manipulation. I define my array outside the function and populate it when the function is called.

$rid = "";
if(isset($_GET['rid'])){
  $rid = $_GET['rid'];
}

$dates = array();    
function getData($rid){
  //Db Connection Strings
  //Query stuff etc

  //DATES
  for ($i=0; $i < 5; $i++) { 
    $mil = (float)$query->table->records->record->f[$i];
    $seconds = $mil / 1000;
    $dt = gmdate('m-d-Y', $seconds);

    $dates[$i] = $dt;
  }  
  print_r($dates);//THIS WORKS
}

getData($rid);
print_r($dates);//THIS DOESN'T

My issue is: Unless I call refer to $dates within the function, it will not spit out my data. I need to have this done outside the function so I can put the values into text fields at run time.

How do I get my values into my $dates array so they can be used globally? Thanks!

Community
  • 1
  • 1
FunkyMonk91
  • 1,469
  • 1
  • 17
  • 30
  • Why don't you add a `$dates` parameter to the `getData` function so that you can call `getData` like `getData($rid, $dates)`? – Joshua Dwire Nov 02 '12 at 19:27
  • 1
    There are probably thousands of questions related to global scope on StackOverflow. Please search for them. Also take a look at the docs related to variable scope. http://php.net/manual/en/language.variables.scope.php and docs about passing variables by reference, which looks like what you are wanting to do. http://php.net/manual/en/language.references.pass.php – Mike Brant Nov 02 '12 at 19:28

4 Answers4

4

There are three basic ways you can do this: passing the array by reference, calling it globally, and returning it from the function.

Pass by reference

function getData($rid, &$dates) {
    ...
    for ($i=0; $i < 5; $i++) { 
        ...
        $dates[$i] = $dt;
    }
}

getData($rid, $dates);

Returning

function getData($rid) {
    $dates = array();
    ...
    for ($i=0; $i < 5; $i++) { 
        ...
        $dates[$i] = $dt;
    }
    return $dates;
}

$dates = getData($rid);

Globals

function getData($rid){
    global $dates;
    ...
    for ($i=0; $i < 5; $i++) { 
        ...
        $dates[$i] = $dt;
    }  
}
FThompson
  • 28,352
  • 13
  • 60
  • 93
  • 1
    More `global` is exactly what PHP needs... – deviousdodo Nov 02 '12 at 19:28
  • @draevor Why does that necessitate a downvote? I answered the exact question. – FThompson Nov 02 '12 at 19:29
  • @FunkyMonk91 Glad to have helped. Don't forget to mark the answer as accepted when you can :) – FThompson Nov 02 '12 at 19:30
  • The question can be answered in multiple ways, I think this option is just bad. – deviousdodo Nov 02 '12 at 19:30
  • 1
    @draevor Please explain your argument against global at the very least for the sake of the questioner. – FThompson Nov 02 '12 at 19:31
  • Why do you think it is bad? It works perfectly in my application, I left out a lot of detail obviously but I don't want to call the function itself as it is querying for much more than just dates. I don't want to do multiple queries to the DB as it isn't exactly the most efficient database and I'm trying to keep my page loads quick. So, in theory I want to do 1 big query, break it up into arrays then spit the data out into my page. – FunkyMonk91 Nov 02 '12 at 19:32
  • 1
    I've added an alternative to using globals--passing the array by reference to the function. Returning the `$dates` array is an option as well, but I decided to show a third option. – FThompson Nov 02 '12 at 19:34
  • Much appreciated Vulcan. I've never had formal training in PHP and I've kind of been thrown into a project with it and a bunch of other technologies I'm not to comfortable with yet! In about 3 min when I can check your answer I will :) – FunkyMonk91 Nov 02 '12 at 19:36
  • 1
    @FunkyMonk91 The disadvantages to global state have been discussed in many places. You can start with Wikipedia for example http://en.wikipedia.org/wiki/Global_variable If what you need is caching, you can do that in various ways - for the simplest look at my response, you can reuse a variable with the return value of the function. – deviousdodo Nov 02 '12 at 19:37
  • 3
    I've reformatted my answer to include all three options for the sake of completeness. – FThompson Nov 02 '12 at 19:39
  • @draevor When I have more time I will definitely look into this. I am on a time limit however and this works as I need it to! I have taken note of this and it's on my to do list for PHP research. Thanks! – FunkyMonk91 Nov 02 '12 at 19:40
  • 1
    @FunkyMonk91 Unfortunately, by that time probably some other poor chap will have to maintain this. To keep it simple, here's only one of the huge problems with globals: from now on you will never be able to use a $dates variable in your project. If you do (and you certainly will at some point), you won't know when one will override the other. Maybe this will make you more interested in doing the reading sooner. – deviousdodo Nov 02 '12 at 19:52
  • Isn't this getData($rid, &$dates) instead of getData($rid, @$dates) ? – AKS Nov 02 '12 at 19:57
  • @draevor That variable is only taken up in this page no? This is simply a page that pulls a ton of data from a database and presents it to the user – FunkyMonk91 Nov 02 '12 at 20:21
2

This is a scoping issue. You should either pass dates in as a reference or return the dates array as a result of the function.

function getData($rid){
   //Do whatever you need to do.
   return $dates;
}


$dates = getData($rid);
print_r($dates);
jjs9534
  • 475
  • 2
  • 7
2

Just return from the function instead of relying on global data:

function getData($rid) {
    // ...
    return $dates;
}
print_r(getData($rid));

The $dates before the function is useless, move it inside the function to initialize it properly.

deviousdodo
  • 9,177
  • 2
  • 29
  • 34
1

Declare the following within your function:

global $dates;

This will use the global variable dates instead of creating a local variable and using that, which is what is happening right now.

Florin Stingaciu
  • 8,085
  • 2
  • 24
  • 45