title: Undefined variable in PHP when the variable is present
body: <p>I have looked at other Stack&nbsp;Overflow questions and none of them fixed the issue. The following class defines the variable and then calls the variable in the HTML code.</p>
  
  <p>I've used this same method for another PHP page and it works perfectly. For some reason it does not work on this page. I've done it like this numerous times without any issues. <a href="https://en.wikipedia.org/wiki/PhpStorm" rel="nofollow noreferrer">PhpStorm</a> says the variable <strong>$user</strong> "might not have been defined", but I know it is. How can I fix this?</p>
  
  <p>I have tried setting the variable global at the top of the page without luck. I have tried setting the variable global after if(isset()) and still doesn't work. Here's my PHP code:</p>
  
  <pre><code>&lt;?php
      session_start();
      require_once 'includes/class.user.php';
      $user_edit = new USER();
  
      if(!$user_edit-&gt;is_logged_in())
      {
          $user_edit-&gt;redirect('index.php');
      }
  
      if(isset($_GET['user_id'])) {
          $tid = $_GET['user_id'];
          $stmt = $user_edit-&gt;runQuery("SELECT * FROM users WHERE user_id = :id");
          $stmt-&gt;execute(array(':id' =&gt; $tid));
          $user = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
      }
  ?&gt;
  
  &lt;?php include('header.php'); ?&gt;
  
      &lt;div class="container side-collapse-container"&gt;
          &lt;p&gt;&lt;?php if (!empty($user['username'])) {
                  echo $user['username'];
              } ?&gt;&lt;/p&gt;
      &lt;/div&gt;
  
  &lt;?php include('footer.php'); ?&gt;
  </code></pre>
  
  <p>This is what is shown in the browser after running it with XAMPP</p>
  
  <blockquote>
    <p>Notice: Undefined variable: user in C:\Users\USER\PhpstormProjects\site1\view_user.php on line 22</p>
  </blockquote>