Problems with Variable Scope using Include_Once
Posted: Wed Sep 20, 2006 11:07 am
Hello,
I am fairly new to programming PHP, however I have knowledge of a few other languages.
I am sure that there is a simple solution to this problem, but I may just be unable to see it.
Details about my setup...
PHP Version: 5.0.0
Display Errors: On
Error Level: E_ALL
Register Globals: Off
The Problem...
My thought is that the problem is to do with the scope of the a variable, but I have read the PHP article on variable scope and I am still facing the same problem.
I have a main file (index.php) and a config file (config.inc.php) both in site root. I also have a connections file (connection.inc.php) in an include folder below the root.
I am trying to include the config file to set all the site variables and then include the connections file to create a connection object for use by the rest of the index page. I am getting an error "Undefined variable: link" from index.php. The code below shows the code snippet for each file...
config.inc.php
index.php
connection.inc.php
I am pretty sure that with this code, the reason for the error is due to the fact that the variable is defined in the included file and therefore that is where the scope of the variable ends. From reading the variable scope document, my understanding was that if the keyword "global" was added to the definition inside the included file, it would make the variable accessible in the index.php page.
As this does not work, I would appreciate some help in this matter.
Many thanks
I am fairly new to programming PHP, however I have knowledge of a few other languages.
I am sure that there is a simple solution to this problem, but I may just be unable to see it.
Details about my setup...
PHP Version: 5.0.0
Display Errors: On
Error Level: E_ALL
Register Globals: Off
The Problem...
My thought is that the problem is to do with the scope of the a variable, but I have read the PHP article on variable scope and I am still facing the same problem.
I have a main file (index.php) and a config file (config.inc.php) both in site root. I also have a connections file (connection.inc.php) in an include folder below the root.
I am trying to include the config file to set all the site variables and then include the connections file to create a connection object for use by the rest of the index page. I am getting an error "Undefined variable: link" from index.php. The code below shows the code snippet for each file...
config.inc.php
Code: Select all
$CONF = array ();
// Site Details - Name and URL
$CONF["Site_URL"] = 'http://localhost/test/';
$CONF["Site_Name"] = "Site Name";Code: Select all
require_once("config.inc.php");
require_once($CONF["Site_URL"] . "include/connection.inc.php");
$sqlCommand = "SELECT intMenuItemID, varMenuText FROM menu";
$result = mysql_query($sqlCommand, $link);Code: Select all
$host = "localhost";
$user = "php";
$password = "php";
$database = "test";
$link = mysql_connect($host, $user, $password) or die("Connection to database could not be made");
mysql_select_db($database, $link);As this does not work, I would appreciate some help in this matter.
Many thanks