Home > Article > Backend Development > Thinkphp c method usage example_PHP tutorial
1. The role of the C method
a. Load and set the user's configuration and save it in the static variable $_config within a C function
b. Read user configuration (read from $_congig)
2. Demand analysis:
1. Set variables
1. Two-dimensional array
C('DB.USER_NAME','XIAOCHEN);
2. One-dimensional array
C(array('USER_NAME'=>'chen','USER_HEIGHT'=>'170'));
2. Read variables
One dimension: C('USER_NAME');
Two-dimensional: C('DB.DB_PASSWORD');
3. During debugging, view all configuration information
C();
3. Storage method and why should it be stored in this way?
First let’s look at a problem $arr=array('db'=>'mysql','DB'=>'mysql','Db'=>'mysql'); From this array we can I see that db points to mysql, but it occupies three storage spaces. The development of the project is not completed by one person, and everyone's writing habits may be different, so in order to avoid this situation, the unified subscript transfer is lowercase (of course uppercase is also acceptable), since the array in the configuration file has only two dimensions at most, it is enough to lowercase the subscript of the one-dimensional array
4. How is it used in actual combat?
Since PHP operates on arrays very conveniently, the configuration file is generally written in a configuration file and returned in the form of an array
The general format is:
Write the variable into C: C(include 'config.php'); After writing, C('DB') can get the value
5. Written by the author (new function of dynamically adding 2D configuration)
Code example: