php database configuration files are generally Shared

  • 2020-05-17 05:02:21
  • OfStack

config. php file:
 
<?php 
$db_name="test"; 
$db_username="root"; 
global $db_password; 
?> 

Database operation class (call configuration file) db.fun.php:
 
<?php 
require("config/config.php"); 
class db{ 
function fun(){ 
global $db_username,$db_password; 
echo " Database user name: ".$db_username."<br />"; 
echo " Database password: ".$db_password."<br />"; 
} 
} 
?> 

Application file test.php:
 
<?php 
require("include/db.fun.php"); 
$a= new db(); 
$a->fun(); 
?> 

global keywords:
 
<?php 
$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 in this range. You may have noticed a slight difference between PHP's global variables and C's. In C, global variables automatically take effect in functions unless overwritten by local variables. This can cause some problems. Some people may inadvertently change a global variable. Global variables in PHP must be declared as global when used in a function.
 
<?php 
$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 references to any variable will point to the global variable. PHP has no limit on the maximum number of global variables that a function can declare.

Related articles: