Introduction: Save the database user name, password, etc. in the configuration file config.php, and then use include or require where needed to include them, and use the global keyword in the function to globalize the variables storing the database name, password, etc. It can be used in functions in the file.
Introduction: Save the database username, password, etc. in the configuration file config.php, then use include or require where needed, and use the global keyword in the function to make the variables storing the database name, password, etc. global ized, it can be used in functions in the file. 1. Configuration file -config.php: 2. Database operation class (call configuration file) db.fun.php: 3. Application file test.php: There will be no output from this script 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 slightly different from C language. In C language, global variables automatically take effect in functions unless overridden by local variables. This can cause problems and may inadvertently change a global variable. Global variables in PHP must be declared global when used in functions. 4. Global file calling example 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. Programmer's home, dedicated to you. |