title: PHP: Set a global variable from within a function
body: <p>I have the following code</p>
  
  <pre><code>$user;
  if (isset($_POST['submitLogin']))
  { 
      $GLOBALS['user'] = logIn();
      // location1
  }
  
  function logIn()
  {
      $user = new User("my username", "my email");
      return $user;
  }
  
  // location2
  </code></pre>
  
  <p>Then I want to display that information like this:</p>
  
  <pre><code>echo $GLOBALS['user']-&gt;__get('username');
  </code></pre>
  
  <p>but it only works in location1.
  I have worked around this by using a global array and passing the data from the User instance to that global array, but this defeats the purpose of using classes.
  If you could just give me a hint or put me on the right track I'd be very thankful.</p>