Home  >  Article  >  Backend Development  >  PHP database configuration file implementation method example

PHP database configuration file implementation method example

WBOY
WBOYOriginal
2016-07-25 08:54:201087browse
  1. $db_name="test";
  2. $db_username="root";
  3. global $db_password;
  4. ?>
Copy code

2, database operation class (call configuration File) db.fun.php:

  1. require("config/config.php");
  2. class db{
  3. function fun(){
  4. global $db_username,$db_password;
  5. echo "Database username: ".$ db_username."
    ";
  6. echo "Database password:".$db_password."
    ";
  7. }
  8. }
  9. ?>
Copy code

3, apply File test.php:

  1. require("include/db.fun.php");
  2. $a= new db();
  3. $a->fun();
  4. ?>
Copy the code

4, global keyword:

  1. $a = 1; /* global scope */
  2. function Test()
  3. {
  4. echo $a; /* reference to local scope variable */
  5. }
  6. Test();
  7. ?>
Copy code

This script will not produce 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. 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 may cause some problems, someone may change a global variable. Global variables in PHP must be declared global when used in functions.

  1. $a = 1;
  2. $b = 2;
  3. function Sum()
  4. {
  5. global $a, $b;
  6. $b = $a + $b;
  7. }
  8. Sum();
  9. echo $b;
  10. ?>
Copy code

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.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn