title: What is the best way to reuse code in PHP?
body: <h3>Code:</h3>
  <pre><code>if ( $_GET['tab'] == 'newest' ) { 
        // Go through each question
        foreach( array_reverse( $end_array, true ) as $tags_and_Qid['question_id'] =&gt; $titles_and_Qid['title'] )
        {   
          // Grab the title for the first array
          $title = $titles [ $tags_and_Qid['question_id'] ] ['title'];
  
          // Grab the tags for the question from the second array
          $tags = $end_array [ $tags_and_Qid['question_id'] ] ['tag'];
  
          // Grab the username for the question from the second array
          $username = $usernames [ $tags_and_Qid['question_id'] ] ['username'];
          --- cut ----                                                                                                                                                       
        }   
    }
  </code></pre>
  <p>I need to use this code often. The only difference is the <code>array_reverse (..., true)</code> in the first example.</p>
  <p>I have tried to solve the problem by making a function <code>organize_question</code> to solve this problem. I was unsuccessful:</p>
  <pre><code>function organize_questions ( $tab ) {
        if ( $_GET['tab'] == 'newest' ) {
          echo ( &quot;array_reverse ( $end_array ,  true )&quot; ); 
                                    // Problem here!
        }
        if ( $_GET['tab'] == 'oldest' ) {
            echo ( &quot;$end_array&quot; );    
              // this does not work
        } else {
          echo ( &quot;array_reverse ( $end_array ,  true )&quot; );
                                     // Problem here!
        }
    }
  </code></pre>
  <p>I then changed the relevant line in my code to this:</p>
  <pre><code> foreach( organize_question( $tab ) as $tags_and_Qid['question_id'] =&gt; $titles_and_Qid['title'] )
  </code></pre>
  <p>The problem is in transferring variables from one function to another.<br />
  I tried to put all necessary variables in the parameters of the function, but everything gets broken, since there are many dependencies on this function.</p>
  <p>I am new to PHP so there must be easier way to do this than what I am trying.</p>