Home > PHP Framework > ThinkPHP > How to use thinkphp 3.23 c method

How to use thinkphp 3.23 c method

藏色散人
Release: 2022-12-06 09:29:18
Original
1865 people have browsed it

The c method in thinkphp3.23 is the method used by ThinkPHP to set, obtain, and save configuration parameters. It is used frequently; its usage syntax is such as "C('DB_NAME','thinkphp');" Indicates that the value of the DB_NAME configuration parameter is set to thinkphp.

How to use thinkphp 3.23 c method

The operating environment of this tutorial: Windows 7 system, thinkphp version 3.23, Dell G3 computer.

thinkphp 3.23 Detailed explanation of C method

thinkphp 3.23:

C method is the method used by ThinkPHP to set, obtain, and save configuration parameters. Use The frequency is higher.

To understand the C method, you need to first understand the configuration of ThinkPHP, because all operations of the C method are related to the configuration. ThinkPHP's configuration file is defined in PHP array format.

Due to the function overloading design, there are many usages. Let’s explain them one by one.

Set parameters

C('DB_NAME','thinkphp');
Copy after login

means setting the value of the DB_NAME configuration parameter to thinkphp. Since the configuration parameters are not case-sensitive, the following writing method is the same: [-more-]

C('db_name','thinkphp');
Copy after login

But it is recommended to keep the configuration definition specifications in uniform capital letters.

All parameters of the project can be dynamically changed through this method before taking effect. The last set value will overwrite the definition in the previous settings or conventional configuration. You can also use the parameter configuration method to add new configurations.

Supports the setting of secondary configuration parameters, for example:

C('USER.USER_ID',8);
Copy after login

It is not recommended that configuration parameters exceed the second level.

If you want to set multiple parameters, you can use batch settings, for example:

$config['user_id'] = 1;
$config['user_type'] = 1;
C($config);
Copy after login

If the first parameter of the C method is passed into the array, it means batch assignment. The above assignment is equivalent to:

C('USER_ID',1);
C('USER_TYPE',1);
Copy after login

Get parameters

To get the set parameters, you can use:

$userId = C('USER_ID');
$userType = C('USER_TYPE');
Copy after login

If the USER_ID parameter has not been defined, NULL will be returned.

It can also support obtaining secondary configuration parameters, for example:

$userId = C('USER.USER_ID');
Copy after login

If the incoming configuration parameters are empty, it means obtaining all parameters:

$config = C();
Copy after login

Save settings

Version 3.1 adds a function to permanently save setting parameters, which is only for batch assignment. For example:

$config['user_id'] = 1;
$config['user_type'] = 1;
C($config,'name');
Copy after login

After setting config parameters in batches, all current configuration parameters will be saved to the cache. file (or other configured caching method).

After saving, if you want to retrieve the saved parameters, you can use

$config = C('','name');
Copy after login

where name is the cache identifier used when saving the parameters earlier, and must be consistent to correctly retrieve the saved parameters. The retrieved parameters will be merged with the current configuration parameters, without manual merging.

TP5:

The system configuration parameters are globally accessed through static variables, and the access method is simple and efficient.

Assistant functions are provided in TP5

config: Get and set configuration parameters

Recommended learning: "thinkPHP Video Tutorial"

The above is the detailed content of How to use thinkphp 3.23 c method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template