The php.ini File Controls the Behavior of PHP - dummies

The php.ini File Controls the Behavior of PHP

By Steve Suehring, Janet Valade

The behavior of PHP is controlled through an initialization file called php.ini. Settings such as how sessions are handled, how errors are displayed, and what modules are available are all controlled through the php.ini file.

The actual location of the php.ini file varies depending on the operating system and how PHP was installed.

    How to work with the php.ini

The php.ini file is a plain text file and should be edited with a plain text editor such as Notepad, Textpad, or Vi.

A good practice is to make a copy of the current php.ini before you start your edits. Doing so makes it easy to revert to the original copy if you discover your changes caused a problem.

When you make a change to the php.ini, you should reload the Apache web server in order to activate the changes.

    How to make changes outside of the php.ini

Changes you make to the php.ini apply globally, to all sites on a server. However, there are times when you want to apply a change either to a site or to an individual page. When this occurs, you have several options, two of which we discuss here.

    Use .htaccess or Apache configuration

Some systems allow you to use an .htaccess file to set PHP options. Alternatively, if you control the server you can make a site-level change within the Apache VirtualHost container.

The php_value directive applies changes to the PHP configuration. For example, if you had a site that needed to upload large files, you could set the upload_max_filesize PHP directive like so:

php_value upload_max_filesize 100M

The directive won’t be applied server-wide, but rather, only to the files or site to which the php_value directive applies. When you use an .htaccess file, the change is applied immediately. If you make the change in the Apache configuration file, then the Apache server needs to be reloaded for the change to take effect.

    Make changes in PHP

PHP offers two configuration-related functions that are useful for this discussion: ini_get() and ini_set(). The ini_get() function retrieves the current value of a given configuration directive, and ini_set() sets the value. For example: