Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8766825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:38:48+00:00 2026-06-13T16:38:48+00:00

Possible Duplicate: PHP global variable not found in functions I have a function that

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:38:50+00:00Added an answer on June 13, 2026 at 4:38 pm

    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;
        }  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: php date compare I have a date that I take from the
Possible Duplicate: global variables in php not working as expected I have a php
Possible Duplicate: php false place in condition I have noticed that a lot of
Possible Duplicate: php loop through associative arrays i have a variable which prints an
Possible Duplicate: Overriding global php.ini file I have looked but can't seem to find
Possible Duplicate: PHP : Array key containing special character not found I am comparing
Possible Duplicate: PHP call class in class returns error:500 I have code that looks
Possible Duplicate: php mail() function on localhost I am using php mail(). I have
Possible Duplicate: PHP - send file to user I have written a script that
Possible Duplicate: PHP global in functions Using something like this worries me: <? global

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.