title: PHP Require() does not execute required file in the scope of parent script
body: <p>I'm trying to offload the process of connecting to a database to a DBConfig.php file, as suggested pretty much everywhere, because I'll need to reuse the code in several files.</p>
  
  <p>The suggestion is always framed as (edited to reflect my actual code):</p>
  
  <pre><code>-------DBConfig.php-------
  &lt;?php
  $link = mysql_connect('localhost','user','pass');
  if (!$link) {
    die('Not connected : ' . mysql_error());
  }
  $db_selected = mysql_select_db('db_name');
  if (!$db_selected) {
      die ('Can\'t use db_name : ' . mysql_error());
  }
  ?&gt;
  
  -------Parent.php-------
  &lt;!DOCTYPE html&gt;
  &lt;?php
  require_once 'DBConfig.php';
  $something = mysql_query("SELECT * FROM `table` LIMIT 0, 30 ");
  // This query works fine if the mysql_connect() and
  // mysql_select_db() stuff is in this script in place
  // of the require_once.
  $listofthings = array();
  while($temp = mysql_fetch_array($something)){
      $listofthings[] = $temp;
  }
  // Do other things too
  ?&gt;
  </code></pre>
  
  <p>But when I try to do <code>mysql_fetch_array($something)</code>, it fails with the warning: <code>mysql_fetch_array() expects parameter 1 to be resource, boolean given</code>.</p>
  
  <hr>
  
  <p>Keeping in mind that everything works beautifully if I simply drop the contents of <code>DBConfig.php</code> into the parent script instead of <code>require 'DBConfig.php';</code>...</p>
  
  <p>And also noting that <code>print(require 'DBConfig.php'); //- 1</code> (PHP is returning a 1 to indicate that it is successfully locating and including the file. See <a href="http://php.net/manual/en/function.include.php" rel="nofollow">example 5</a>)...</p>
  
  <p>What is going on, and how do I fix it? I am using the default configuration for <a href="http://www.wampserver.com/en/" rel="nofollow">WAMPServer</a> (Apache 2.4.2, PHP 5.4.3), running on Windows 7 x64.</p>