PHP Manual
Prev |  | Next

# 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:

__FILE__

  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.

__LINE__

  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.

PHP_VERSION

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

PHP_OS

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

TRUE

  A true value.

FALSE

  A false value.

E_ERROR

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

E_WARNING

  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().

E_PARSE

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

E_NOTICE

  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.

The E_* constants are typically used with the error_reporting() function for setting the error reporting level.

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!");
    ?>

Prev | Home | Next
Variables from outside PHP | Up | Expressions