PHP database configuration file implementation method example

WBOY
Release: 2016-07-25 08:54:20
Original
1076 people have browsed it
  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.



source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!