-1

I have the following codes and they are not working:

index.php:

    include("loadData.php");
    $my_var = loadData("myTxt.txt");
    var_dump($my_var);

loadData.php:

    function loadData($my_file){
       if(file_exists($my_file)){
          $file_contents = file_get_contents($my_file);
          $file_contents = json_decode($file_contents);
       }else{
          // If file doesn't exist, creates the file and runs the function again
          $data_to_insert_into_file = simplexml_load_file("http://site_with_content.com");
          $fp = fopen($my_file, "w");
          fwrite($fp, json_encode($data_to_insert_into_file));
          fclose($fp);
          // Since the file is created I will call the function again
          loadData($my_file);
          return;
       }

       // Do things with the decoded file contents (this is suposed to run after the file is loaded)
       $result = array();
       $result = $file_contents['something'];
       return $result;
    }

This works as expected in the second time (after the file is created), I can display info on index.php, but in the first time I run (before the file is created) it always displays $result as NULL, I can't understand why since I call the function again...

Any idea?

Thank you

1 Answers1

2

You don't return anything when you do your fetch:

if (...) {
   $file_contents = file_get_contents(...);
   // no return call here
} else {
   ...
   return; // return nothing, e.g. null
}
return $result; // $result is NEVER set in your code

You should have return $file_contents. Or better yet:

if (...) {
   $result = get cached data
} else {
   $result = fetch/get new data
}
return $result;

by using the proper variable names everywhere.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • I have edited the function, I forgot to write the last two lines before the return $result! – user2894688 Jul 07 '14 at 16:28
  • you still don't return ANYTHING from your fetch/fwrite section, you simply `return;`, which means the calling context gets a null value. – Marc B Jul 07 '14 at 16:31
  • But when I call the function again won't I exit the "current" function? (Stop the current operation) – user2894688 Jul 07 '14 at 16:32
  • 1
    no. and why are you doing this recursively anyways? There is **NO** point in fetching the file, writing it to disk, and then calling the function AGAIN just to read the exact same data from disk. – Marc B Jul 07 '14 at 16:35
  • I don't want to bother the site again, now that I have the file I want to load it from my own server. How would you do it? Thank you. – user2894688 Jul 07 '14 at 16:37
  • don't even bother recursing. `if (file_exists() { return data from file } else { fetch data from site, write data to file, return data }` only if the file doesn't exist do you hit the site. – Marc B Jul 07 '14 at 16:38
  • Why shouldn't this be recursive? I still don't get it sorry. – user2894688 Jul 07 '14 at 16:43
  • because you already HAVE your text when you fetch it from the site and write it to disk. it's like having a chocolate bar in your hand and deciding to go back to the store to buy another chocolate bar. just eat the one you already have. – Marc B Jul 07 '14 at 16:44
  • No, in the first time that I run the function the file never exist, $my_file is dynamic. – user2894688 Jul 07 '14 at 16:45
  • so? You fetch the data into a variable, you write to file from that variable. That data is *STILL* in the same variable you fetched it into, so just return from that. – Marc B Jul 07 '14 at 16:47
  • I do alot of things with the decoded data after the return, I will rethink this function. – user2894688 Jul 07 '14 at 16:54