Home > Article > Backend Development > Master the correct way to reference configuration files
1. The use of global
As we all know, global variables
cannot be used without modification in methods, as follows:
<?php $a=1; function Test() { echo $a;//报错,undefined variable } ?>
So We need help from global
:
<?php $a=1; function Test() { global $a; echo $a;//报错,undefined variable } Test();//输出1 ?>
2. Introduce the configuration file
The configuration file is as follows:
<?php //在根目录下config\config.php return array( //数据库配置 'database'=>array( 'type'=>'mysql', 'host'=>'localhost', 'port'=>'3306', 'user'=>'root', 'pass'=>'root', 'charset'=>'utf8', 'dbname'=>'my_database', 'prefix'=>'' ), 'system'=>array( 'error_reporting'=>E_ALL,//错误级别控制,默认显示所有错误 'display_errors'=>1, //错误显示控制,1代表显示错误,0代表隐藏错误; ) ); ?>
is used as follows:
<?php //在根目录下 function abc(){ global $config; $config=include './config/config.php'; } function efg(){ global $config; var_dump($config); } abc();//调用abc()方法,将配置文件赋值引入 efg();//输出配置文件 echo "<br>"; echo "<pre class="brush:php;toolbar:false">"; var_dump($config); ?>
Recommended: php tutorial,php video tutorial
The above is the detailed content of Master the correct way to reference configuration files. For more information, please follow other related articles on the PHP Chinese website!