Zend PHP 7 Certification – Basics – Configuration

This post covers the Configuration section of the PHP Basics chapter when studying for the Zend PHP 7 Certification.

Configuration files establish the initial settings for applications, as well as servers and operating systems. For PHP, configuration resides in the php.ini file.

php.ini is read when PHP starts up. For the server module versions of PHP, this happens only once when the web server is started. For the CGI and CLI versions, it happens on every invocation.

There are several locations in which php.ini can reside, and the file is searched for in these locations in this order:

  • SAPI module specific location
  • The PHPRC environment variable
  • The following registry keys are examined in order: [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y.z], [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x.y] and [HKEY_LOCAL_MACHINE\SOFTWARE\PHP\x], where x, y and z mean the PHP major, minor and release versions.
  • [HKEY_LOCAL_MACHINE\SOFTWARE\PHP], value of IniFilePath (Windows only).
  • Current working directory (except CLI).
  • The web server’s directory
  • Windows directory

You can find out the loaded php.ini file by either running the phpinfo() function, or by running the php_ini_loaded_file() function.

The php.ini file will contain a lot of configuration directives that can be changed to suit your application needs. Resource limits within the file may look like the following:

max_execution_time = 30 ; Maximum execution time of each script, in seconds
max_input_time = 60	; Maximum amount of time each script may spend parsing request data
memory_limit = 128M     ; Maximum amount of memory a script may consume

Note that the semi-colon represents a comment in this file.

Session configuration settings are also set here.

session.save_handler = files
session.save_path = /some/save/path

; Whether to use cookies.
session.use_cookies = 1

You can also use the ini_set() function within PHP scripts to set values of given configuration options. The configuration option will keep this new value during the script’s execution, and will be restored at the script’s ending.

ini_set() takes two parameters: the configuration variable name (i.e the directives set in php.ini) and the new value that you would like the variables to be changed to. So to increase the memory limit for a given script, use ini_set() at the top of the file:

ini_set('memory_limit', '512M');

Or to set the save session path to a different directory.

ini_set('session.save_path', '/path/to/your/folder');

.user.ini

Within PHP there is also a .user.ini file. As of PHP version 5.3 this file file allows you to customise PHP directives on a per-directory basis.

Two new INI directives, user_ini.filename and user_ini.cache_ttl control the use of user INI files.

user_ini.filename sets the name of the file PHP looks for in each directory; if set to an empty string, PHP doesn’t scan at all. The default is .user.ini.

user_ini.cache_ttl controls how often user INI files are re-read. The default is 300 seconds (5 minutes).

It should be noted that these files are processed only by the CGI/FastCGI SAPI. If you are using Apache, then you can use Apache configuration files, such as httpd.conf or .htaccess files for the same effect.

Here is a sample code snippet for changing the memory_limit environment variable within .htaccess.

<IfModule mod_php7.c>
php_value memory_limit 768M
</IfModule>

View the other sections:

Note: This article is based on PHP version 7.0