Russian Ukraine English
Catalog RSS

Chapter 8. Constants

PHP defines several constants and provides a mechanism for defining more at run-time. Constants are much like variables, save for the two facts that constants must be defined using the define() function, and that they cannot later be redefined to another value.

The predefined constants (always available) are:


The name of the script file presently being parsed. If used within a file which has been included or required, then the name of the included file is given, and not the name of the parent file.


The number of the line within the current script file which is being parsed. If used within a file which has been included or required, then the position within the included file is given.


The string representation of the version of the PHP parser presently in use; e.g. '3.0.8-dev'.


The name of the operating system on which the PHP parser is executing; e.g. 'Linux'.


A true value.


A false value.


Denotes an error other than a parsing error from which recovery is not possible.


Denotes a condition where PHP knows something is wrong, but will continue anyway; these can be caught by the script itself. An example would be an invalid regexp in ereg().


The parser choked on invalid syntax in the script file. Recovery is not possible.


Something happened which may or may not be an error. Execution continues. Examples include using an unquoted string as a hash index, or accessing a variable which has not been set.


All of the E_* constants rolled into one. If used with error_reporting(), will cause any and all problems noticed by PHP to be reported.

The E_* constants are typically used with the error_reporting() function for setting the error reporting level. See all these constants at Error handling.

You can define additional constants using the define() function.

Note that these are constants, not C-style macros; only valid scalar data may be represented by a constant.

Example 8-1. Defining Constants

<?php define("CONSTANT", "Hello world."); echo CONSTANT; // outputs "Hello world." ?>      

Example 8-2. Using __FILE__ and __LINE__

<?php function report_error($file, $line, $message) {     echo "An error occured in $file on line $line: $message."; }  report_error(__FILE__,__LINE__, "Something went wrong!"); ?>