title: PHP - eval code from DB
body: <p>there have been hundreds if not thousands of posts concerning the use of PHP's eval(); to run code from a database. Through all my searching I have not found an answer to my question (explained shortly).</p>
  
  <p>Firstly I'll introduce you to my application.</p>
  
  <p>I have three records of valid code stored in a database:<br>
  eg:<br>
  ['code1']</p>
  
  <pre><code>$num1 = 1;
  $num2 = 3;
  $num3 = $num1+$num2;  //4
  </code></pre>
  
  <p>['code2']</p>
  
  <pre><code>$num4 = $num3;        //4
  $num5 = 5;
  $num6 = $num4+$num5;  //9
  </code></pre>
  
  <p>['code3']</p>
  
  <pre><code>$num7 = $num4;        //4
  $num8 = $num6;        //9
  $num9 = $num7+$num8;  //13
  echo $num9;           //13
  </code></pre>
  
  <p>Next I have a function to call and run a record:<br>
  eg:</p>
  
  <pre><code>function runCode($codeName) {
      // assume db connection is established
      $result = mysql_query("SELECT `code` FROM CodeStore WHERE `name` = '".$codeName."'");
      if ($result) {
          // Fetch one row
          $row = mysql_fetch_assoc($result);
          if (!$row) {
              die('No rows returned.');
          } else {
              return eval($row['code']);
          }
      } else {
          die('Invalid query: '.mysql_error());
      }
  }
  </code></pre>
  
  <p>Now, what needs to happen is to call the three above snippets, one after each other, and have the variables inside ($numX) available for use between each other.<br>
  eg:</p>
  
  <pre><code>runCode('code1');
  runCode('code2');
  runCode('code3');
  </code></pre>
  
  <p>The above call of the three snippets from the db should echo '13', it does not. And there is my question:</p>
  
  <p>How can I make these variables available outside the eval'd code?</p>