title: PHP function is not returning data
body: <p>I have an array of data that is created by a function, but the function will not return the data.</p>
  
  <p>Here is the function:</p>
  
  <pre><code>/**
   * Takes in a legacy id and returns new id.
   */
  function my_function($data) {
    foreach ($data as $info) {
      $new_info = my_other_function($info);
      $new_data[] = $new_info;
    }
  
    return $new_data;
  }
  </code></pre>
  
  <p>Here is where it's called:</p>
  
  <pre><code>$data = array('7245');
  echo 'test1';                   // prints out test1
  $new_data = my_function($data);
  print_r($new_data);                 // DOES NOT PRINT ANYTHING, SAME RESULTS WITH VAR_DUMP()
  echo 'test2';                   // prints out test2
  </code></pre>
  
  <p>However if I change the function to the following, it prints out while in the function, but still does not print out the returned value:</p>
  
  <pre><code>function my_function($data) {
    foreach ($data as $info) {
      $new_info = my_other_function($info);
      $new_data[] = $new_info;
    }
    print_r($new_data);
    return $new_data;
  }
  </code></pre>
  
  <p>So now this:</p>
  
  <pre><code>$data = array('7245');
  echo 'test1';                   // prints out test1
  $new_data = my_function($data);
  echo 'test2';                   // prints out test2
  print_r($new_data);                 // DOES NOT PRINT ANYTHING, SAME RESULTS WITH VAR_DUMP()
  </code></pre>
  
  <p>Prints out this:</p>
  
  <pre><code>test1
  array(0=&gt;9876)
  test2
  // RETURNED VALUE SHOULD BE HERE BUT IT IS NOT
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Since it seems this is essientally impossible, I'm adding the original function, which lives in a Drupal module.  I have test all other functions called in this function and they work perfectly on their own.</p>
  
  <pre><code>$file_data = '7024';
  $ids = tdm_migration_import_file($file_data, $inline='no');
  
  function tdm_migration_import_file($data, $inline) {
    // create base array
    $new_data = array();
    if (!is_array($data)) {
      $data = array($data);
    }
    // if we are reprocessing this data, remove the first element (flag) of
    // the array
    // dpm($data, 'incoming data');
    if (isset($data['reprocess'])) {
      unset($data['reprocess']);
    }
    foreach ($data as $file_data) {
      if (trim($file_data) == '') {
        continue;
      }
      // check to see if this data is coming from a text block or not
      if ($inline == 'no') {
        // given a path or fid
        if (is_numeric($file_data)) {
          // given an int (fid)
          $legacy_fid = $file_data;
          $legacy_file_path = tdm_migration_get_legacy_file_data($legacy_fid);
          // set the flag to reprocess this data
          $new_data['reprocess'] = 'yes';
          // add file path to new data for reprocessing
          $new_data[] = $legacy_file_path;
        } else {
          // given a string (path or uri/url)
          $legacy_file_path = $file_data;
          $path_data = tdm_migration_extract_path_data($legacy_file_path);
          // // if the directory doesn't exist we create it and make sure permissions
          // allow for writing by the server
          file_prepare_directory($path_data['public_path'], FILE_CREATE_DIRECTORY);
          // check to see if the file already exists in file structure
          $realpath = drupal_realpath($path_data['public_file_path']);
          if (!file_exists($realpath)) {
            // create new file and get new fid
            $fid = tdm_migration_create_file($path_data['legacy_file_url'], $path_data['public_file_path'], $data='fid');
          } else {
            // get the existing file id
            $fid = tdm_migration_get_existing_file_data($path_data['public_file_path']);
          }
          $new_data[] = $fid;
        }
      } else {
        // given a body of text, find &lt;img&gt; tags and extract the src attributes
        $legacy_paths = tdm_migration_extract_img_srcs($file_data);
        $replacement_paths = array();
        foreach ($legacy_paths as $legacy_path) {
          $path_data = tdm_migration_extract_path_data($legacy_path);
          file_prepare_directory($path_data['public_path'], FILE_CREATE_DIRECTORY);
          $realpath = drupal_realpath($path_data['public_file_path']);
          if (!file_exists($realpath)) {
            $new_path = tdm_migration_create_file($path_data['legacy_file_url'], $path_data['public_file_path'], $data='path');
          } else {
            $new_path = $public_file_path;
          }
          // aggregate old and new paths
          $replacement_paths[$legacy_path] = $new_path;
        }
        // replace all old paths with new paths in original text
        $new_file_data = tdm_migration_replace_text($file_data, $replacement_paths);
        $new_data[] = $new_file_data;
      }
    }
    if (isset($new_data['reprocess']) &amp;&amp; $new_data['reprocess'] == 'yes') {
      // dpm($new_data, 'reprocessing');
      tdm_migration_import_file($new_data, $inline='no');
    } else {
      dpm($new_data, 'new-data'); // THIS PRINTS OUT!!!!!!
      return $new_data;
    }
  }
  </code></pre>
  
  <p>EDIT2: There seems to be some confusion as to what this is doing so I will explain the steps in plain english.</p>
  
  <ol>
  <li>$data = '7204'; The data variable is being set to a string of numbers</li>
  <li>Place $data in an array, also called $data.</li>
  <li>Check to see if the key 'reprocess' is set in $data, if it is, remove it</li>
  <li>Itterate through the $data array, individual elements of the array now called $file_data (this function downloads files based on migration needs).</li>
  <li>If the element is blank, skip it and go to the next element</li>
  <li>Check if the $inline variable is set, if it is we are dealing with HTML (not the issue at hand)  In this case the data is a string of numbers.  $inline is never set to 'yes'.</li>
  <li>Check to see if the element is numeric, in this case IT IS NUMERIC</li>
  <li>Get the relative path, stored in the old database, using tdm_migration_get_legacy_file_data().  The new data looks something like: sites/default/files/somefile.jpg</li>
  <li>Set reprocess to yes in the new_data</li>
  <li>Add the returned file path to the new_data array.</li>
  <li>Now the function goes to the bottom with the last if/else and checks if reprocess isset and if it is equal to yes - in this case IT IS!</li>
  <li>Since reprocess is equal to yes, we run through the function again with the given string (path)!</li>
  <li>UNSET reprocess!</li>
  <li>Itterate through the data again</li>
  <li>Not inline</li>
  <li>Not numeric!  <strong>SO REPROCESS IS NEVER SET NOW!!!</strong> </li>
  <li>Get path data (returns things like the new server path, etc) - verified working</li>
  <li>Prepare the directory with write privleges and create it if it doesn't already exist</li>
  <li>Check if the file already exists on the server</li>
  <li>If it doesn't, then it creates the file and adds the file info to the database by running tdm_migration_create_file(), which returns the file id ($fid).  - verified working  - If it does exist, it just checks the database for the file id - verified working</li>
  <li>Add the file id to $new_data.</li>
  <li>Now at the bottom of the function, it goes to the <strong>ELSE because REPROCESS IS NOT SET</strong> and should return the data</li>
  </ol>
  
  <p>This last part is what is wrong.  If add the line dpm($new_data, 'new-data'); before the return, it prints out exactly what it should.  For the uninitiated, dpm is a drupal fucntion that prints out data in a clean format.  If it change the dpm line to var_dump($new_data); die(); - it STILL prints the data, but the function WILL NOT RETURN IT!</p>