Problems with Variable Scope using Include_Once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
playuk
Forum Newbie
Posts: 4
Joined: Wed Sep 20, 2006 10:28 am

Problems with Variable Scope using Include_Once

Post by playuk »

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

Code: Select all

$CONF = array ();

// Site Details - Name and URL
$CONF["Site_URL"] = 'http://localhost/test/';
$CONF["Site_Name"] = "Site Name";
index.php

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);
connection.inc.php

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);
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 :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

A file is not a scope boundary. An include works almost as if the code in the included file stands at the position of the include() statement.

Is either
$link = mysql_connect($host, $user, $password) or die("Connection to database could not be made");
mysql_select_db($database, $link);
or
$link = mysql_connect($host, $user, $password) or die("Connection to database could not be made");
mysql_select_db($database, $link);
within a function/method?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

If you use require_once() on a file starting with "http://" it'll open the file through an http wrapper, essentially the same sort of thing as if you'd requested http://www.google.com or ftp://www.mywebsite.co.uk. You need to use require_once() using a local filesystem name, eg ./includes/connection.inc.php.
playuk
Forum Newbie
Posts: 4
Joined: Wed Sep 20, 2006 10:28 am

Post by playuk »

Code: Select all

$link = mysql_connect($host, $user, $password) or die("Connection to database could not be made");
mysql_select_db($database, $link);
There is no function / method defined within this file.

I have tried giving the include_once a local filesystem path instead, but I face the same problem.
playuk
Forum Newbie
Posts: 4
Joined: Wed Sep 20, 2006 10:28 am

Post by playuk »

OK, thanks for the help...

For some reason I think my local install of PHP and apache was having a funny 5 minutes because when I have run the same code today, it is now working...

That is computers for you :)
Post Reply