Home  >  Article  >  Backend Development  >  Detailed introduction to the ini file related operating functions of php

Detailed introduction to the ini file related operating functions of php

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-07-02 15:24:081624browse

In small companies, especially entrepreneurial companies, the establishment of the entire server is generally one of the responsibilities of our PHP development engineers. Among them, the most important one is to configure the server's php.ini file. Some parameters will have a profound impact on the performance of the server, and some parameters can be dynamically specified and obtained when PHP is running. Today, we will learn some operating functions related to the php.ini file.

Detailed introduction to the ini file related operating functions of php

Dynamicly set the configuration parameters of the ini file

I believe this function will be familiar to everyone, and basically anyone who has done PHP development will have used it. However, some parameters cannot be modified. Did you know this?

ini_set('allow_url_fopen', 0);
echo ini_get('allow_url_fopen'), PHP_EOL; // 1 ,无法修改,PHP_INI_SYSTEM

ini_set('memory_limit', -1);
echo ini_get('memory_limit'), PHP_EOL; // -1,可以修改,PHP_INI_ALL

Please pay attention to the comments. The first comment says PHP_INI_SYSTEM, and this parameter cannot be modified. Yes, I believe you are smart and have figured out that these parameters have corresponding types. PHP_INI_SYSTEM means that it can only be modified in php.ini or httpd.conf, and cannot be modified when the language is running dynamically.

There are four types of different php.ini configuration parameters:

  • PHP_INI_USER: It can be set in user scripts (such as ini_set()) or Windows registry (since PHP 5.3 onwards) and set

  • PHP_INI_PERDIR in .user.ini: You can set

  • ## in php.ini, .htaccess or httpd.conf ##PHP_INI_SYSTEM: Can be set in php.ini or httpd.conf

  • PHP_INI_ALL: Can be set anywhere

That is Say, using ini_set() we can set parameters of type PHP_INI_USER and PHP_INI_ALL, while the other two can only be set and modified in php.ini or other configuration files. For the specific types corresponding to configuration parameters, please refer to the relevant PHP documentation.

https://www.php.net/manual/zh/ini.list.php

Get the configuration information in the ini file

Of course, read php. There are no restrictions on the configuration information in the ini file. It can be read directly. We can use two functions to read, they are: get_cfg_var() and ini_get(). In addition, there is a function ini_get_all() that can obtain configuration information in the form of an array collection. Let’s look at them one by one.

get_cfg_var() and ini_get()

Both read single configuration parameter information.

echo get_cfg_var('error_reporting'), PHP_EOL; // 32759
echo ini_get('error_reporting'), PHP_EOL; // 32759

echo get_cfg_var('request_order'), PHP_EOL; // GP
echo ini_get('request_order'), PHP_EOL; // GP

// php.ini A=TEST_A
echo get_cfg_var('A'), PHP_EOL; // TEST_A
echo ini_get('A'), PHP_EOL; //

No need to explain the above two items. What we need to pay attention to is the last one. We define a custom configuration parameter A in the php.ini file. It can be seen that get_cfg_var() can obtain this information normally, but ini_get() cannot. Let's look at another example.

ini_set('error_reporting', E_WARNING);
echo get_cfg_var('error_reporting'), PHP_EOL; // 32759,只返回.ini的内容
echo ini_get('error_reporting'), PHP_EOL; // 2,返回当前配置运行时的状态

After using ini_set() to dynamically set the error_reporting parameter, get_cfg_var() returns the value set by ini_set(), while ini_get() still obtains the value configured in the php.ini file.

You can see the difference between these two functions from the above two examples:

  • get_cfg_var(), you can get customized configuration parameter values, but only in php .ini file shall prevail. Dynamically modified parameter values ​​cannot be obtained.

  • ini_get() cannot obtain customized configuration parameter values. The current dynamic script runtime configuration shall prevail. That is to say, the modified parameter value of ini_set() can be obtained

ini_get_all()

It obtains a set of data, such as some extensions we installed Swoole, Configuration information such as xDebug or mysqlnd.

print_r(ini_get_all('swoole'));
echo PHP_EOL;
// Array
// (
//     [swoole.display_errors] => Array
//         (
//             [global_value] => On
//             [local_value] => On
//             [access] => 7
//         )

//     [swoole.enable_coroutine] => Array
//         (
//             [global_value] => On
//             [local_value] => On
//             [access] => 7
//         )

//     [swoole.enable_library] => Array
//         (
//             [global_value] => On
//             [local_value] => On
//             [access] => 7
//         )

//     [swoole.enable_preemptive_scheduler] => Array
//         (
//             [global_value] => Off
//             [local_value] => Off
//             [access] => 7
//         )

//     [swoole.unixsock_buffer_size] => Array
//         (
//             [global_value] => 262144
//             [local_value] => 262144
//             [access] => 7
//         )

//     [swoole.use_shortname] => Array
//         (
//             [global_value] => 
//             [local_value] => 
//             [access] => 4
//         )

// )

It can be seen that all the configuration information we made for Swoole is returned in the form of an array.

Restore configuration information

After we use ini_set() to dynamically set parameter information, if we want to restore it to the default configuration in the php.ini file, we can directly use an ini_restore() function. That's it.

ini_restore('error_reporting');
echo ini_get('error_reporting'), PHP_EOL; // 32759

Still using the above code, error_reporting has been modified by us to 2. At this time, we directly use ini_restore() to restore, and then use ini_get() to see that the error_reporting parameter is restored back to php The original value defined in the .ini file.

Get the currently loaded configuration file path

When you take over a server, the first step is often to find its related application configuration files, such as mysql's my.ini or nginx's conf related configuration file path, and in PHP our first step is to find where the php.ini file is.

echo php_ini_loaded_file(), PHP_EOL;
// /usr/local/etc/php/7.3/php.ini

echo php_ini_scanned_files(), PHP_EOL;

We directly use php_ini_loaded_file() to easily obtain the path of the php.ini file loaded in the currently running script environment. The php_ini_scanned_files() function returns all paths that can scan the php.ini file in comma-separated form.

In fact, these two parameters are reflected in phpinfo(), but many times we cannot use phpinfo() directly in the production environment. Compared to these two functions or phpinfo(), a better solution is to find the location of the php.ini file directly on the command line.

php --ini
# Configuration File (php.ini) Path: /usr/local/etc/php/7.3
# Loaded Configuration File:         /usr/local/etc/php/7.3/php.ini
# Scan for additional .ini files in: /usr/local/etc/php/7.3/conf.d
# Additional .ini files parsed:      /usr/local/etc/php/7.3/conf.d/ext-opcache.ini

php -i | grep "Configuration"
# Configuration File (php.ini) Path => /usr/local/etc/php/7.3
# Loaded Configuration File => /usr/local/etc/php/7.3/php.ini
# Configuration

phpinfo()

Regarding phpinfo(), we don’t need to explain too much. What’s in it should be a required course for developers who learn to use PHP. Here, we just introduce the parameters of the phpinfo() function. Yes, it has parameters and can only display part of the information instead of all of it.

  • INFO_GENERAL:配置的命令行、 php.ini 的文件位置、建立的时间、Web 服务器、系统及更多其他信息。

  • INFO_CREDITS:PHP 贡献者名单。参加 phpcredits()。

  • INFO_CONFIGURATION:当前PHP指令的本地值和主值。参见 ini_get()。

  • INFO_MODULES:已加载的模块和模块相应的设置。参见 get_loaded_extensions()。

  • INFO_ENVIRONMENT:环境变量信息也可以用 $_ENV 获取。

  • INFO_VARIABLES:显示所有来自 EGPCS (Environment, GET, POST, Cookie, Server) 的 预定义变量。

  • INFO_LICENSE:PHP许可证信息。参见 » license FAQ。

  • INFO_ALL:显示以上所有信息。

phpinfo(INFO_MODULES);

上面的代码在页面中所显示的信息就只是已加载模式相关的配置信息了。phpinfo() 会直接输出到页面上,如果想将它的内容保存在一个变量中,我们需要使用输出缓冲控制来进行操作。我们将在后面的文章中讲到这方面的内容。这里就简单的给一段代码。

ob_start();
phpinfo();
$v = ob_get_contents();
ob_end_clean();

echo $v;

总结

不看不知道,一看吓一跳。原来只是使用过 ini_set() 去修改运行时内存大小,但直到今天才知道原来 ini_set() 并不是所有的配置都可以修改的,每个参数是否能动态修改还要看它的参数类型。

而且上面还忘了说了,我们并不能使用 ini_set() 去增加配置参数。也就是说,使用 ini_set("B", "TEST_B") 增加一个 B 参数,然后直接使用 ini_get() 也是无法获取的。

而且简单的获取参数信息的两个函数也有这么多的不同,phpinfo() 原来也有这么多参数。果然,文档才是最好的学习资料。旅程还没有停止,我们刷文档的脚步依然不能停,一起加油冲冲冲!!

测试代码:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202005/source/%E5%85%B3%E4%BA%8Ephp%E7%9A%84ini%E6%96%87%E4%BB%B6%E7%9B%B8%E5%85%B3%E6%93%8D%E4%BD%9C%E5%87%BD%E6%95%B0%E6%B5%85%E6%9E%90.php

推荐学习:php视频教程

The above is the detailed content of Detailed introduction to the ini file related operating functions of php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete