title: How to see and return results of a function used inside an if statement?
body: <p>I wrote this function and I'm using it to upload an image to my local server dynamically. </p>
  
  <p>I ran into the following issue, the <code>$image_name</code> is generated inside it, so when I call the <code>uploadImage function</code> I can't return the <code>$image_name</code> so that I can insert it into the database, I'm not sure how to return that variable.</p>
  
  <pre><code>public function uploadImage($data, $uploadLocation, $nameTag){
      if($data['size'] != 0) {
          $errors     = array();
          $maxsize    = 16777216;
          $acceptable = array(
              'image/jpeg',
              'image/jpg',
          );
          $image_extension = pathinfo($data['name'], PATHINFO_EXTENSION);
  
          //image_name variable I'm referring to
          $image_name = uniqid($nameTag, true) . '.' . $image_extension;
  
          if($data['size'] &gt;= $maxsize) {
              $errors[] = 'File too large. File must be less than 16 megabytes.';
          } else if($data['size'] == 0){
              $errors[] = 'You need to upload an image.';
          }
  
          if((!in_array($data['type'], $acceptable)) || (empty($data['type']))) {
              $errors[] = 'Invalid file type. Only JPG, GIF and PNG types are accepted.';
          }
  
          if(count($errors) === 0) {
              $moveFile = move_uploaded_file($data['tmp_name'], $uploadLocation . '/' . $image_name );
  
              if($moveFile){
                  return true;
              }
          }
      }
  
      return false;
  }
  </code></pre>
  
  <p>Here I'm using the uploadImage function.</p>
  
  <pre><code>$uploadImage = new UploadImages();
  
  if($uploadImage-&gt;uploadImage($data['image_data'], 'uploads/img/instructions', 'instruction_')){
  
      //here I'd like to return the $image_name from the function
      //I'm using PDO to insert the name in database
      $sth = $db-&gt;prepare('UPDATE instructions SET image = :image WHERE id = :id');
      $sth-&gt;bindValue(':image', //name returned from the function, PDO::PARAM_STR );
      $sth-&gt;bindValue(':id', $instructionsId, PDO::PARAM_INT);
      $sth-&gt;execute();
  }
  </code></pre>
  
  <p>I figured that in the piece of code:</p>
  
  <pre><code>if($moveFile){
      return true;
  }
  </code></pre>
  
  <p>I could return the <code>$image_name</code> instead of true, but I'm unsure how I'd catch it to be usable since it's inside an if statement.</p>
  
  <p>Any ideas how we can return that specific data, or suggestion in how to better rewrite the code to fit those needs would be great.</p>