title: Variable inside recursive function change in for-loop
body: <p>I have a variable which is an argument of a recursive function, and its value change in the code without warning. I echo <code>$idParent</code> before and in a <code>for</code>, and the value is different.</p>
  
  <pre><code>public function sortCategoriesByLevel($categories, $levelMax, $idParent, $currentLevel = 0) {
      $sortedCategories = array();
      $categoriesFiltered = array();
  
      //here is what i don't understand : 
      echo ' idparent sent to fonction : '.$idParent; //output the id
      for($i = 0; $i &lt; count($categories); $i++) {
          echo '&lt;br /&gt; idParent in "for" : ';
          var_dump($idParent); //output -1 no matter what!!!!
  
          echo '&lt;br /&gt; isset idparent : ';
          echo isset($idParent);
  
          $level = $categories[$i]-&gt;level;
          echo '&lt;br /&gt; RUN &lt;br /&gt;';
  
          if($level == $currentLevel) {
              echo '&lt;br /&gt; TRAITEMENT &lt;br /&gt;';
              $id = $categories[$i]-&gt;id;
              echo 'idparent before condition : '.$idParent.' id : '.$id;
  
              if($idParent == $id || $idParent == -1) {
                  $sortedCategories[$currentLevel][$categories[$i]-&gt;id][0] = $categories[$i];
                  echo '&lt;br /&gt; ENREGISTREMENT &lt;br /&gt;';
              } else {
                  echo '&lt;br /&gt; SAUVEGARDE &lt;br /&gt;';
                  $categoriesFiltered[] = $categories[$i];
              }
  
  
              /*echo '&lt;br /&gt;';
              var_dump($categoriesFiltered);
              echo '&lt;br /&gt;';*/
  
              if($currentLevel &lt; $levelMax) {
                  echo '&lt;br /&gt; APPEL &lt;br /&gt;';
                  $currentLevel++;
                  $sortedCategories[$currentLevel][$categories[$i]-&gt;id][1][] = $this-&gt;sortCategoriesByLevel($categoriesFiltered, $levelMax, $id, $currentLevel);
              }
          }
      }
      return $sortedCategories;
  }
  </code></pre>
  
  <p>The result is:</p>
  
  <p>idparent sent to fonction : -1<br>
  idParent in "for" : int(-1)<br>
  isset idparent : 1<br>
  RUN   </p>
  
  <p>TRAITEMENT<br>
  idparent before condition : -1 id : 18<br>
  ENREGISTREMENT   </p>
  
  <p>APPEL<br>
  <strong>idparent sent to fonction : 18</strong><br>
  <strong>idParent in "for" : int(-1)</strong><br>
  isset idparent : 1<br>
  RUN   </p>
  
  <p>I have put in bold the part I don't understand.</p>