Heim  >  Artikel  >  Backend-Entwicklung  >  php数据库配置文件实现方法示例

php数据库配置文件实现方法示例

WBOY
WBOYOriginal
2016-07-25 08:54:201087Durchsuche
  1. $db_name="test";
  2. $db_username="root";
  3. global $db_password;
  4. ?>
复制代码

2,数据库操作类(调用配置文件)db.fun.php:

  1. require("config/config.php");
  2. class db{
  3. function fun(){
  4. global $db_username,$db_password;
  5. echo "数据库用户名:".$db_username."
    ";
  6. echo "数据库密码:".$db_password."
    ";
  7. }
  8. }
  9. ?>
复制代码

3,应用文件test.php:

  1. require("include/db.fun.php");
  2. $a= new db();
  3. $a->fun();
  4. ?>
复制代码

4,global关键字:

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

这个脚本不会有任何输出,因为 echo 语句引用了一个局部版本的变量 $a,而且在这个范围内,它并没有被赋值。 PHP 的全局变量和 C 语言有一点点不同,在 C 语言中,全局变量在函数中自动生效,除非被局部变量覆盖。 这可能引起一些问题,有些人可能改变一个全局变量。PHP 中全局变量在函数中使用时必须申明为全局。

  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. ?>
复制代码

以上脚本的输出将是“3”。 在函数中申明了全局变量 $a 和 $b,任何变量的所有引用变量都会指向到全局变量。 对于一个函数能够申明的全局变量的最大个数,PHP 没有限制。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn