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.
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
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; //
ini_set('error_reporting', E_WARNING); echo get_cfg_var('error_reporting'), PHP_EOL; // 32759,只返回.ini的内容 echo ini_get('error_reporting'), PHP_EOL; // 2,返回当前配置运行时的状态
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 // ) // )
ini_restore('error_reporting'); echo ini_get('error_reporting'), PHP_EOL; // 32759
echo php_ini_loaded_file(), PHP_EOL; // /usr/local/etc/php/7.3/php.ini echo php_ini_scanned_files(), PHP_EOL;
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
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!