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 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:
You can find out the loaded php.ini file by either running the
phpinfo() function, or by running the
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.
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:
Or to set the save session path to a different directory.
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.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.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
.htaccess files for the same effect.
Here is a sample code snippet for changing the
memory_limit environment variable within
php_value memory_limit 768M
View the other sections:
Note: This article is based on PHP version 7.0