Php database configuration file implementation example

Source: Internet
Author: User
Php database configuration file implementation example

  1. $ Db_name = "test ";
  2. $ Db_username = "root ";
  3. Global $ db_password;
  4. ?>

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. ?>

3. application file test. php:

  1. Require ("include/db. fun. php ");
  2. $ A = new db ();
  3. $ A-> fun ();
  4. ?>

4. global keyword:

  1. $ A = 1;/* global scope */
  2. Function Test ()
  3. {
  4. Echo $ a;/* reference to local scope variable */
  5. }
  6. Test ();
  7. ?>

This script does not have any output, because the echo statement references a local version of variable $ a and is not assigned a value within this range. The global variables in PHP are a little different from those in C language. in C language, global variables automatically take effect in functions unless they are overwritten by local variables. This may cause some problems. some may change a global variable. Global variables in PHP must be declared as 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. ?>

The output of the above script is "3 ". Specify the global variables $ a and $ B in the function. all referenced variables of any variable point to the global variable. PHP has no limit on the maximum number of global variables that a function can declare.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.