[ Team LiB ] Previous Section Next Section


After you have compiled or installed PHP, you can still change its behavior with a file called php.ini. On Unix systems, the default location for this file is /usr/local/lib; on a Windows system, the default location is the Windows directory. You should find a sample php.ini file in your distribution directory, which contains factory settings. Factory settings are used if no php.ini file is used. Directives in the php.ini file come in two forms: values and flags. Value directives take the form of a directive name and a value separated by an equals sign. Possible values vary from directive to directive. Flag directives take the form of directive name and a positive or negative term separated by an equals sign. Positive terms can be 1, On, Yes, and True; negative terms can be 0, Off, No, and False. White space is ignored.

If PHP has been preinstalled on your system, you might want to check some of the settings in php.ini.

You can change your php.ini settings at any time; however, if you are running PHP as an Apache module, you should restart the server for the changes to take effect.


The short_open_tag directive determines whether you can begin a block of PHP code with the symbol <? and close it with ?>. If this has been disabled, you will see one of the following:

short_open_tag = Off
short_open_tag = False
short_open_tag = No

To enable the directive, you can use one of the following:

short_open_tag = On
short_open_tag = True
short_open_tag = Yes

You can read more about PHP open and close tags in Hour 3, "A First Script."

Error Reporting Directives

To diagnose bugs in your code, you should enable the directive that allows error messages to be written to the browser. This is on by default:

display_errors = On

You should turn this off for production code—that is, code that is displayed to the general public. The reason for this is that error messages displayed on the browser can give away weaknesses in your code to potential attackers. For production code, you should log errors instead, like so:

log_errors = On

You can also set the level of error reporting. For the examples you'll be working through in this book, you should set this to the following:

error_reporting = E_ALL & ~E_NOTICE

This will report all errors, apart from notices. This setting is the default and gives you a good sense of what is happening in your scripts.

Variable Directives

PHP makes certain variables available to you as a result of a GET request, POST request, or cookie. You can influence this in the php.ini file.

The register_globals directive determines whether values resulting from an HTTP request should be made available as global variables. This is now officially deprecated, and register_globals is set to off by default:

register_globals = Off

None of the scripts in this edition of Sams Teach Yourself PHP in 24 Hours rely on this directive, and you should keep it disabled.

Changing php.ini Directives Locally

If you are running Apache with the module version of PHP and your configuration allows the use of the .htaccess file, you can enable and disable php.ini directives on a per-directory basis.

Within the .htaccess file you can use the php_flag directive to set a php.ini flag (a directive that requires 'On' or 'Off') and the php_value directive to set a php.ini value (a directive that requires a string or number):

php_flag  short_open_tag  On
php_value include_path ".:/home/corrdev"

If you are not running Apache, all is not lost. As of PHP 4.0.5, the function ini_set() was introduced. It enables you to set some php.ini directives from within your code. ini_set() requires two strings—the directive name, and the value to set:

ini_set( "include_path", ".:/home/corrdev" );

You can read more about functions in Hour 6, "Functions."

    [ Team LiB ] Previous Section Next Section