PHP Predefined Constants

In PHP, predefined constants are built-in constants that are always available and do not require defining. They are used to get information about PHP itself, the environment, errors, and more. Below is a categorized list of common predefined constants:


1. PHP Version Constants

  • PHP_VERSION – Current PHP version (e.g., 8.1.0)

  • PHP_MAJOR_VERSION – Major version (e.g., 8)

  • PHP_MINOR_VERSION – Minor version (e.g., 1)

  • PHP_RELEASE_VERSION – Release version (e.g., 0)

  • PHP_VERSION_ID – Version as an integer (e.g., 80100)

  • PHP_EXTRA_VERSION – Extra version info (e.g., -dev)


2. OS and Environment

  • PHP_OS – Operating system PHP was built on

  • PHP_OS_FAMILY – OS family (e.g., Windows, Linux)

  • PHP_SAPI – Server API (e.g., apache2handler, cli)

  • DIRECTORY_SEPARATOR – OS-specific directory separator (/ on UNIX, \ on Windows)

  • PATH_SEPARATOR – Path separator (: on UNIX, ; on Windows)

  • PHP_EOL – End of line character for the platform


3. File System Constants

  • __FILE__ – Full path and filename of the file

  • __DIR__ – Directory of the file

  • __LINE__ – Current line number of the file

  • __FUNCTION__ – Current function name

  • __CLASS__ – Current class name

  • __TRAIT__ – Trait name

  • __METHOD__ – Method name

  • __NAMESPACE__ – Current namespace name


4. Error Handling Constants

  • E_ERROR, E_WARNING, E_PARSE, E_NOTICE, etc. – Different error types

  • E_ALL – All errors and warnings (useful for error_reporting())


5. Miscellaneous

  • DEFAULT_INCLUDE_PATH – Default include path

  • ZEND_THREAD_SAFE – True if thread safety is enabled

  • PHP_INT_MAX, PHP_INT_MIN – Max and min integer values

  • PHP_FLOAT_MAX, PHP_FLOAT_MIN – Max and min float values