In small companies, especially startups, setting up the entire server is often one of our PHP developers’ responsibilities. One of the most important is to configure the server’s php.ini file. Some parameters can have a profound impact on server performance, and some parameters can be specified and retrieved dynamically at PHP runtime. Today, we’ll look at some of the manipulation functions associated with php.ini files.

Dynamically set the configuration parameters of the INI file

This function I believe you will not be unfamiliar, basically do PHP development will use. However, some parameters cannot be modified, did you know that?

ini_set('allow_url_fopen'.0);
echo ini_get('allow_url_fopen'), PHP_EOL; // 1, cannot be modified, PHP_INI_SYSTEM

ini_set('memory_limit', -1);
echo ini_get('memory_limit'), PHP_EOL; // -1, can be modified, PHP_INI_ALL
Copy the code

Notice that the first comment says PHP_INI_SYSTEM, and this parameter cannot be changed. That’s right, as you’re smart enough to see, there are types for these parameters. PHP_INI_SYSTEM can only be modified in php.ini or httpd.conf, not at runtime.

Different php.ini configuration parameter pairs should have four types:

  • PHP_INI_USER: can be set in user scripts (such as ini_set()) or Windows registries (since PHP 5.3) as well as.user.ini
  • PHP_INI_PERDIR: can be 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, with ini_set() we can set parameters of type PHP_INI_USER and PHP_INI_ALL, while the other two can only be set and changed in php.ini or other configuration files. For details about the types of configuration parameters, see related PHP documents.

www.php.net/manual/zh/i…

Obtain the configuration information in the. Ini file

Of course, there are no restrictions on reading the configuration information in the php.ini file. It can be read directly, and we can use two functions to do this: get_cfg_var() and ini_get(). There is also a function, ini_get_all(), that gets configuration information in the form of an array collection. Let’s take it one by one.

Get_cfg_var () and ini_get ()

Both read information about a single configuration parameter.

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; // 
Copy the code

The first two need no explanation, but the last one needs to be noted. We define A custom configuration parameter A in the php.ini file. As you can see, get_cfg_var() gets this information normally, but ini_get() does not. Let’s look at another example.

ini_set('error_reporting', E_WARNING);
echo get_cfg_var('error_reporting'), PHP_EOL; // 32759, only the contents of.ini are returned
echo ini_get('error_reporting'), PHP_EOL; // 2, returns the status of the current configuration runtime
Copy the code

After the error_reporting parameter is set dynamically with ini_set(), get_cfg_var() returns the value set by ini_set(), while ini_get() still gets the value configured in the php.ini file.

The two examples above show the difference between the two functions:

  • Get_cfg_var () can be used to obtain customized configuration parameter values. However, only the php.ini file is used to obtain dynamic parameter values
  • Ini_get (), can not get the value of the custom configuration parameter, the current dynamic script running configuration prevail, that is, can get the value of the modified ini_set() parameter

ini_get_all()

It gets a set of data, such as configuration information for extensions like Swoole, xDebug, or mysqlnd that we installed.

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
/ /)

// )
Copy the code

As you can see, all the configuration information we did for Swoole is returned as an array.

Restoring configuration Information

After setting the parameter information dynamically using ini_set(), we simply use an ini_restore() function to restore the default configuration in the php.ini file.

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

Error_reporting has been changed to 2. At this point, we restore it directly using ini_restore(). Using ini_get() again, you can see that the error_reporting parameter is restored to the original value defined in the php.ini file.

Gets the path of the currently loaded configuration file

When you take over a server, the first step is to find its application configuration files, such as mysql my.ini or nginx conf configuration file path. In PHP, the first step is to find the php.ini file.

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

echo php_ini_scanned_files(), PHP_EOL;
Copy the code

Using php_ini_loaded_file(), we can easily get the path to the php.ini file loaded in the currently running script environment. The php_ini_scanned_files() function returns comma-separated paths to all php.ini files that can be scanned. Both parameters are present in phpInfo (), but most of the time you can’t use phpInfo () directly in production.

In fact, a better solution than either of these functions or phpInfo () is to find the location of the php.ini file directly on the command line.

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

php -i | grep "Configuration"
# Configuration File (php.ini) Path => /usr/localThe/etc/PHP / 7.3
# Loaded Configuration File => /usr/localThe/etc/PHP / 7.3 / PHP. Ini
# Configuration
Copy the code

phpinfo()

We don’t need to explain much about phpInfo (), but what it contains should be a required course for developers learning to use PHP. Here, we’ll just go over the parameters of phpInfo (). Yes, it has parameters that allow it to display only part of the information rather than all of it.

  • INFO_GENERAL: command line configuration, php.ini file location, creation time, Web server, system, and more.
  • INFO_CREDITS: List of CONTRIBUTORS to PHP. Attend phpcredits ().
  • INFO_CONFIGURATION: local and primary values of the current PHP directive. See ini_get ().
  • INFO_MODULES: loaded modules and their Settings. See get_loaded_extensions ().
  • INFO_ENVIRONMENT: Environment variable information can also be obtained with $_ENV.
  • INFO_VARIABLES: Displays all predefined variables from EGPCS (Environment, GET, POST, Cookie, Server).
  • INFO_LICENSE: PHP license information. See » License FAQ.
  • INFO_ALL: displays all the preceding information.
phpinfo(INFO_MODULES);
Copy the code

The information displayed in the above code on the page is only configuration information related to the loaded mode. Phpinfo () prints directly to the page, and if we want to save its contents in a variable, we need to use output buffering control to do so. We will cover this in a later article. I’m just going to give you a little code here.

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

echo $v;
Copy the code

conclusion

Don’t see don’t know, a look startled. We used ini_set() to change the runtime memory size, but until today not all configurations of ini_set() can be changed, and each parameter can be changed dynamically depending on its parameter type. And I forgot to mention that we can’t use ini_set() to add configuration parameters. That is, adding a B argument with ini_set(“B”, “TEST_B”) and then using ini_get() directly is also not available. And the two functions that simply get parameter information are so different that phpInfo () used to have so many parameters. Sure enough, documentation is the best source of learning. The journey has not stopped, we can not stop the pace of brushing documents, together refueling rush!!

Test code:

Github.com/zhangyue050…

Reference Documents:

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/i…

www.php.net/manual/zh/c…