title: Global variables getting reset in PHP
body: <p>I have a script with 3 (relevant) global variables used for keeping track of errors that happen while I'm checking user data. The function that handles them looks like this:</p>
  
  <pre><code>$errors = '';
  $errorNo = 0;
  $erred = false;
  
  function addError($errorMessage) {
      global $errors, $errorNo, $erred;
      if (!$erred) {
          $erred = true;
      }
      $errorNo++;
      $errors .= $errorNo . ". " . $errorMessage;
  }
  </code></pre>
  
  <p>Assume I call <code>addError()</code> more than once, <code>$errors</code> will have a value of <code>''</code> even though it was just modified by the <code>addError()</code> function. Not only that, but the variables <code>$errorNo</code> and <code>$erred</code> are also being reset to the values I first initialised them with.</p>
  
  <p>I checked my code thoroughly to see if <code>$error</code> or the other variables were being modified somewhere else but to no avail.</p>
  
  <p>Now, here's were it gets even weirder. To make sure that they weren't being modified by anything else (I have a external library imported using <code>require()</code>) I made a class called <code>errorHandler</code>.</p>
  
  <pre><code>class errorHandler {
      private $errorCount = 0;
      private $erred = false;
      private $errors = '';
  
      public function addError($errorMessage) {
          $this-&gt;erred = true;
          $this-&gt;errorCount++;
          $this-&gt;errors .= $this-&gt;errorCount . ". " . $errorMessage;
      }
  
      public function getErrors() {
          return $this-&gt;errors;
      }
  }
  </code></pre>
  
  <p>The values of <code>$errors</code>, <code>$errorCount</code> and <code>$erred</code> are not being stored after the function finishes. If I echo the value inside the function it will be the value I just assigned to it, however if I echo it prior to the assignment, it will be the value it had when it was initialised.</p>
  
  <p>Any ideas where the problem might be? Thanks.</p>
  
  <p>EDIT :</p>
  
  <p>The <code>errorHandler</code> class is used as follows:</p>
  
  <pre><code>$errorHandler = new errorHandler; // Earlier in code
  
  function addError($errorMessage) { // Function has name to avoid renaming
      global $errorHandler;
      $errorHandler-&gt;addError($errorMessage);
  }
  </code></pre>