In PHP, predefined constants are built into the language and available for use in any script without the need for manual definition. They provide useful information about the current environment, PHP configuration, error handling, and more.
Unlike user-defined constants, predefined ones are always available and can't be changed during runtime.
PHP Version & Environment Constants
These constants help determine the PHP version and environment your script is running in.
# PHP_VERSION - The current PHP version as a string.
# PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION - Version components as integers.
# PHP_VERSION_ID - Numeric version ID (useful for comparisons).
# PHP_OS - Operating system PHP was built on.
# PHP_OS_FAMILY - More generic OS family.
# PHP_SAPI - Server API.
Magic Constants
These aren't traditional constants; their values change depending on context.
# __LINE__ - Current line number.
# __FILE__ - Full path and filename of the file.
# __DIR__ - Directory of the file.
# __FUNCTION__ - Function name.
# __CLASS__ - Class name.
# __METHOD__ - Class method name.
# __NAMESPACE__ - Current namespace.
# __TRAIT__ - Current trait name.
Error Handling Constants
These are used for error reporting and logging.
# E_ERROR - Fatal runtime errors.
# E_WARNING - Runtime warnings.
# E_PARSE - Compile-time parse errors.
# E_NOTICE - Notices.
# E_STRICT - Suggests changes for better code compatibility.
# E_ALL - All errors and warnings.
Filesystem & Path Constants
# DIRECTORY_SEPARATOR - / on Linux/Unix, \ on Windows.
# PATH_SEPARATOR - : on Linux/Unix, ; on Windows.
Execution Environment Constants
These provide details about your PHP execution environment.
# PHP_BINDIR - Path to PHP binary.
# PHP_CONFIG_FILE_PATH - Default php.ini directory.
# PHP_CONFIG_FILE_SCAN_DIR - Directory scanned for additional .ini files.
# PHP_SHLIB_SUFFIX - Extension suffix for shared libraries (dll on Windows, so on Linux).
Standard Input/Output Constants
# STDIN, STDOUT, STDERR - File pointers for console input/output.
Zend Engine Constants
# ZEND_VERSION - Version of the Zend Engine powering PHP.
Upload & Configuration Related Constants
# UPLOAD_ERR_OK - Value 0, no error occurred.
# UPLOAD_ERR_INI_SIZE - File exceeds upload_max_filesize in php.ini.
# UPLOAD_ERR_FORM_SIZE - File exceeds MAX_FILE_SIZE from HTML form.
# UPLOAD_ERR_NO_FILE - No file uploaded.
Tokenizer & PHP Compilation Constants
# T_STRING, T_VARIABLE, etc. - Used with the Tokenizer extension (token_get_all()).
# PHP_EOL - End of line character depending on OS (\n on Linux, \r\n on Windows).
Source: Orkhan Alishov's notes