Guest_imported
New member
- Jan 1, 1970
- 0
Hi there. I have included "config.inc" which contains my database's name,username and password on the top of my page. I wonder why the script wouldn't work properly if I DON'T declare those three variables($dbname,$dbuser,$dbpass) as GLOBAL in every function that involved using database.(as shown below). Can't I write it JUST FOR ONCE in my script? What is the scope of GLOBAL and what is the main purpose of declaring it? Please forgive my inersia as I am still new to php. Helps are very appreciated. Thank you.
//config.inc
<?
$dbname="foobar";
$dbuser="foobar";
$dbpass="foobar";
?>
//testpage.php
<?
include('config.inc');
function use_db_1() {
global $dbname,$dbuser,$dbpass;
..
..
}
function use_db_2() {
global $dbname,$dbuser,$dbpass;
..
..
}
function use_db_3() {
global $dbname,$dbuser,$dbpass;
..
..
}
?>
//config.inc
<?
$dbname="foobar";
$dbuser="foobar";
$dbpass="foobar";
?>
//testpage.php
<?
include('config.inc');
function use_db_1() {
global $dbname,$dbuser,$dbpass;
..
..
}
function use_db_2() {
global $dbname,$dbuser,$dbpass;
..
..
}
function use_db_3() {
global $dbname,$dbuser,$dbpass;
..
..
}
?>