config.php file:
Copy code The code is as follows:
$db_name="test" ;
$db_username="root";
global $db_password;
?>
Database operation class (call configuration file) db.fun.php:
Copy code The code is as follows:
require("config/config.php");
class db{
function fun(){
global $db_username,$db_password;
echo "Database username:".$db_username."
";
echo "Database Password: ".$db_password."
";
}
}
?>
Application file test.php:
Copy code The code is as follows:
require("include/db.fun.php");
$a= new db();
$a->fun();
?>
global keyword:
Copy code The code is as follows:
$a = 1; /* global scope */
function Test()
{
echo $a; /* reference to local scope variable */
}
Test();
?>
This script will not have any output, Because the echo statement refers to a local version of the variable $a, and it is not assigned a value in this scope. You may notice that PHP's global variables are a little different from C language. In C language, global variables automatically take effect in functions unless overridden by local variables. This can cause problems, as someone might carelessly change a global variable. Global variables in PHP must be declared global when used in functions.
Copy code The code is as follows:
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
The output of the above script will be "3". Global variables $a and $b are declared in the function, and all reference variables of any variable will point to the global variables. PHP has no limit on the maximum number of global variables that a function can declare.
http://www.bkjia.com/PHPjc/325677.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325677.htmlTechArticleconfig.php file: Copy the code as follows: ?php $db_name="test"; $db_username="root "; global $db_password; ? Database operation class (call configuration file) db.fun.php: Copy code...