PHP Database configuration file Implementation Method Example

Source: Internet
Author: User
    1. $db _name= "Test";
    2. $db _username= "root";
    3. Global $db _password;
    4. ?>
Copy Code

2, Database operation class (call config file) db.fun.php:

    1. Require ("config/config.php");
    2. Class db{
    3. function Fun () {
    4. Global $db _username, $db _password;
    5. echo "Database user name:" $db _username. "
      ";
    6. echo "Database password:". $db _password. "
      ";
    7. }
    8. }
    9. ?>
Copy Code

3, application file test.php:

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

4,global Keywords:

    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 does not have any output, because the Echo statement refers to a local version of the variable $a, and within that range, it is not assigned a value. PHP has a slightly different global variable from the C language, and in C, global variables are automatically applied in functions unless overridden by local variables. This can cause some problems, and some people may change a global variable. A global variable in PHP must be declared global when used in a function.

    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". The global variable $a and $b are declared in the function, and all reference variables of any variable are pointed to the global variable. PHP has no restrictions on the maximum number of global variables 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.