您可以在某个地方定义此文件并将其包含或要求它。
require_once("path/to/file/config.php");
此文件中的任何变量都可以在脚本中访问
requires
/
includes
它。
或者可以使用define as:
define("TEST", "10"); //TEST holds constant 10
此外,如果希望在函数中访问它们,则有三个选项,在调用时将它们作为参数传递给函数,或在函数中声明为全局。
//example 1
require_once("path/to/file/config.php");
function testFunction($var){
echo $var." inside my function"; //echos contents of $var
}
//now lets say a variable $test = 10; was defined in config.php
echo $test; //displays "10" as it was defined in the config file. all is good
testFunction($test); //displays "10 inside my function" because $test was passed to function
//example 2
require_once("path/to/file/config.php");
function testFunction2(){
global $test;
echo $test; //displays "10" as defined in config.php
}
//example 3
define("TEST", "10");
echo TEST; // outputs "10"
//could have these constants defined in your config file as described and used above also!