title: Call a $index created in a function of another file php
body: <p>I'm trying to call a variable called $index that count the number of a foreach iteration that is inside a function. 
  In this same function, i call another function that needs to get this $index value at the end of the foreach. </p>
  
  <pre><code>       &lt;?php
  
  $index = 0;
  
  function edit_profile_form() {
  
      global $index;
      $current_user_id = get_current_user_id();
      $data = get_user_meta ($current_user_id);
  
      if (isset($_POST['button2'])) {
          delete_user_meta($current_user_id, 'experiences');
      }
  
      if(isset($_POST['button1'])) {
          save_extra_profile_fields($current_user_id, $index);
      };
  
      $experiences = get_user_meta($current_user_id, 'experiences', true);
  
  ?&gt;
  
  [...SOME CODE HERE...]
  </code></pre>
  
  <p>Next in the file (but always in the function), i call this : </p>
  
  <pre><code>if (!empty($experiences)) {
          foreach ($experiences as $key) {
          ?&gt;
              &lt;div class="past_experience"&gt;
                  &lt;div class="experience_header"&gt;
                      &lt;div&gt;
                          &lt;label for="team"&gt;Nom de l'équipe&lt;/label&gt;
                              &lt;input class="team" name="team_&lt;?= $index ?&gt;" value="&lt;?= $key['new_experience_team'];?&gt;"/&gt;
                      &lt;/div&gt;
                      &lt;div&gt;
                          &lt;label for="role"&gt;Rôle dans l'équipe&lt;/label&gt;
                              &lt;input class="role" name="role_&lt;?= $index ?&gt;" value="&lt;?= $key['new_experience_role'];?&gt;"/&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
                  &lt;div class="experience_textarea"&gt;
                      &lt;label for="description"&gt;Description du rôle&lt;/label&gt;
                      &lt;textarea class="description" name="description_&lt;?= $index ?&gt;"&gt;&lt;?= $key['new_experience_description']; ?&gt;&lt;/textarea&gt;
  
                      &lt;label for="palmares"&gt;Palmarés avec l'équipe&lt;/label&gt;
                      &lt;textarea class="palmares" name="palmares_&lt;?= $index ?&gt;"&gt;&lt;?= $key['new_experience_palmares']; ?&gt;&lt;/textarea&gt;
                  &lt;/div&gt;
              &lt;/div&gt;  
          &lt;?php
          $index++;
          } 
  
      } else {
      ?&gt;
      &lt;div&gt;&lt;p&gt;Vous n'avez encore rentré aucune expérience&lt;/p&gt;&lt;/div&gt;
  </code></pre>
  
  <p>And here is now the function called at the beginnig of the file but placed in another file : </p>
  
  <pre><code>function save_extra_profile_fields($user_id, $index) {
  
  if (!empty($_POST['experiences'])) {
      $experiences[] = $_POST['experiences'];
      for ($i = 0; $i &lt; $index; $i++) {
          $experience_{$i}['new_experience_team'] = $_POST["team_{$i}"];
          $experience_{$i}['new_experience_role'] = $_POST["role_{$i}"];
          $experience_{$i}['new_experience_description'] = $_POST["description_{$i}"];
          $experience_{$i}['new_experience_palmares'] = $_POST["palmares_{$i}"];
          $experiences[] = $experience_{$i};
      }
      update_usermeta($user_id, 'experiences', $experiences);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>It's not working because in this function, $index is not defined in fact.</p>
  
  <p>Thanks all</p>