title: How to properly use PHP require
body: <p>I have 3 files: <code>index.php</code>, <code>db.php</code> (database), and <code>functions.php</code></p>
  
  <p>here is an example what is in each file:</p>
  
  <p><strong>database.php:</strong></p>
  
  <pre><code>define ("DB_HOST", "localhost");
  define ("DB_USER", "root");
  define ("DB_PASS", "1234");
  define ("DB_NAME", "test");
  
  try {
      $dsn = "mysql:dbname=".DB_NAME.";host=".DB_HOST;
      $dbh = new PDO($dsn, DB_USER, DB_PASS);
  } catch (PDOException $e) {
      echo 'Connection failed: ' . $e-&gt;getMessage();
  }
  </code></pre>
  
  <p><strong>index.php:</strong></p>
  
  <pre><code>require $_SERVER['DOCUMENT_ROOT']."/config/db.php";
  require $_SERVER['DOCUMENT_ROOT']."/config/functions.php";
  
  if(isLoggedIn()) {
      echo "hi";
  }
  </code></pre>
  
  <p><strong>functions.php:</strong></p>
  
  <pre><code>function isLoggedIn() {
      require $_SERVER['DOCUMENT_ROOT']."/config/db.php";
  
      $stmt = $dbh-&gt;prepare("SELECT * FROM users....");
      $stmt-&gt;execute();
  }
  </code></pre>
  
  <hr>
  
  <p>The error i get is:</p>
  
  <blockquote>
    <p>Notice: Constant DB_HOST already defined in /var/www/config/database.php</p>
  </blockquote>
  
  <p><strong>what i tried:</strong></p>
  
  <p>I tried to replace <code>require</code> with <code>require_once</code> in all my files but the error that it gives is here:</p>
  
  <blockquote>
    <p>Fatal error: Uncaught Error: Call to a member function prepare() on null in functions.php</p>
  </blockquote>