title: PHP set variable in function within switch within foreach
body: <p>I need to declare a bunch of variables within a function based on data which is looped through using a <code>foreach</code> with a <code>switch</code> statement with another <code>foreach</code>. I think I'm misunderstanding the scope of the variables I'm using, any help would be great. </p>
  
  <pre><code>class Users {
  
      public function createUserData(){
          $user = $this-&gt;getUserData(); //not shown function
          $this-&gt;createFullDataSet($user);
      }
  
      private function createFullDataSet($user){
          foreach( $user['meta'][0] as $key =&gt; $value) {
          //variables required later
          $entity_def_id = 0;
          $entity_id = 0;
          $data_def_id = 0;
          $user_id = 0;
          //thats plenty, you get the idea
  
          switch( $key ){
             case "id":
                 //set $user_id to use later
                 $user_id = $value; // &lt;&lt;-- DOESN'T WORK, only works within the case
                 break;
             case "email":
             case "username":
             case //lots of other cases...
                 break;
             case "location":
             case "hometown":
             case "something":
                 //for the last three, the data structure is the same, good test case
                 //foreach starts when certain conditions met, irrelevant for question
                 foreach( $value as $data_key =&gt; $data_value ){
                     $data_type = 'string';
                     if( is_numeric( $data_value )
                         $data_type = 'integer';
  
                     $data_def_id = $this-&gt;createDataDef( some $vars ); //returns an ID using $pdo-&gt;lastInsertId(); ( works as has echo'd correctly, at least within this case )
  
                     $this-&gt;createSomethingElse //with variables within this foreach, works
                 }
                 break;
              } //end of switch
              $this-&gt;createRelation( $data_def_id ); // &lt;&lt;-- DOESN'T WORK!! Empty variable
          }
      }
  
      private function createRelation( $data_def_id ){
          // something awesome happens!
      }
  }
  </code></pre>
  
  <p>As can be seen from the above code, I want to use a variable outside of the switch statement, though it needs to be declared in <code>foreach</code>-><code>switch</code>-><code>foreach</code> due to existing data structure (this data structure is a pain and is why this needs to be done, before anyone asks: no can't "just be changed to make it easier").</p>
  
  <p>Now I've been reading up on the variable scopes of foreach and switch statements ( <a href="https://stackoverflow.com/questions/2306263/php-switch-statement-variable-scope">here</a>, <a href="https://stackoverflow.com/questions/13392132/undefined-variable-do-switch-statements-have-scope-in-php">here</a>, <a href="http://php.net/manual/en/language.variables.scope.php" rel="nofollow noreferrer">here</a> and <a href="http://www.phptutorial.be/variable-scope.html" rel="nofollow noreferrer">here</a> and tried to find more), however none the wiser as to why the <code>$data_def_id</code>, at the start of the function set to <code>0</code>, doesn't get reset to whatever value comes along in the inner <code>foreach</code>. I'm trying to avoid using <code>global</code> variables as some of this functionality will be used in a product.</p>
  
  <p>I need to be able to use the variables within the <code>private function</code>, throughout the whole private function (including the <code>foreach</code>, <code>switch</code>, etc). What am I doing wrong and how do I fix it?  </p>