PHP Database configuration file general practices sharing _php Tips

Source: Internet
Author: User
config.php file:
Copy Code code as follows:

<?php
$db _name= "Test";
$db _username= "root";
Global $db _password;
?>

Database Operations Class (call configuration file) db.fun.php:
Copy Code code as follows:

<?php
Require ("config/config.php");
Class db{
function Fun () {
Global $db _username, $db _password;
echo "Database user name:". $db _username. " <br/> ";
echo "Database password:". $db _password. " <br/> ";
}
}
?>

Apply file test.php:
Copy Code code as follows:

<?php
Require ("include/db.fun.php");
$a = new db ();
$a->fun ();
?>

Global keyword:
Copy Code code as follows:

<?php
$a = 1; /* Global Scope * *
function Test ()
{
echo $a; /* Reference to local scope variable * *
}
Test ();
?>

This script will not have any output because the Echo statement refers to a local version of the variable $a, and within that scope it is not assigned. You may notice that the global variable in PHP is a little bit different from the C language, in which the global variable takes effect automatically in the function unless overridden by a local variable. This can cause some problems, and some people may inadvertently change a global variable. Global variables in PHP must be declared as global when used in functions.
Copy Code code as follows:

<?php
$a = 1;
$b = 2;
function Sum ()
{
Global $a, $b;
$b = $a + $b;
}
Sum ();
Echo $b;
?>

The output from 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 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.