PHP Predefined and Magic Constants Cheat Sheet (PHP 8.4 Ready)
A categorized reference of the most useful constants in PHP with descriptions, error masks, and sample outputs for PHP 8.3 and PHP 8.4.
PHP provides built-in constants for filesystem operations, compile-time metadata, runtime environment information, error levels, and stream handles. These constants are available globally without importing namespaces or requiring setup.
Filesystem and Path Constants
| Constant | Description | Example Output |
|---|---|---|
DIRECTORY_SEPARATOR |
Path separator (/ on Unix, \ on Windows) |
/ |
PATH_SEPARATOR |
Include path separator (: on Unix, ; on Windows) |
: |
__FILE__ |
Full filesystem path and filename of the current file | /var/www/html/index.php |
__DIR__ |
Directory path containing the current file | /var/www/html |
__LINE__ |
Current line number in the executing file | 42 |
Magic Compile-Time Constants
Magic constants are resolved at compile time, introducing zero runtime performance overhead:
| Constant | Description | Example Output |
|---|---|---|
__FUNCTION__ |
Current function name | calculateTotal |
__CLASS__ |
Current class name with namespace | App\Models\Order |
__TRAIT__ |
Current trait name | App\Traits\HasLabel |
__METHOD__ |
Current class method name | App\Services\PaymentService::charge |
__NAMESPACE__ |
Current namespace | App\Services |
PHP Environment and System Limits
| Constant | Description | Example Output |
|---|---|---|
PHP_VERSION |
Full PHP version string | 8.4.1 |
PHP_MAJOR_VERSION |
Major version integer | 8 |
PHP_MINOR_VERSION |
Minor version integer | 4 |
PHP_RELEASE_VERSION |
Patch release integer | 1 |
PHP_OS |
Operating system string from PHP build | Linux, WINNT, Darwin |
PHP_OS_FAMILY |
Portable operating system group | Linux, Windows, BSD, Darwin |
PHP_SAPI |
Server API interface type | cli, fpm-fcgi, apache2handler |
PHP_EOL |
Correct newline character for the host OS | \n or \r\n |
PHP_INT_MAX |
Maximum integer supported by host architecture | 9223372036854775807 |
PHP_INT_MIN |
Minimum integer supported by host architecture | -9223372036854775808 |
PHP_FLOAT_MAX |
Maximum floating-point number | 1.7976931348623E+308 |
PHP_FLOAT_MIN |
Minimum positive normalized floating-point number | 2.2250738585072E-308 |
Error Reporting Masks
| Constant | Description | Category |
|---|---|---|
E_ERROR |
Fatal runtime errors that halt execution | Fatal |
E_WARNING |
Non-fatal runtime warnings | Warning |
E_PARSE |
Compile-time parse/syntax errors | Fatal |
E_NOTICE |
Runtime notices about potential issues | Notice |
E_CORE_ERROR |
Fatal errors generated by PHP core startup | Fatal |
E_CORE_WARNING |
Warnings generated by PHP core startup | Warning |
E_COMPILE_ERROR |
Fatal errors generated by the Zend script engine | Fatal |
E_COMPILE_WARNING |
Warnings generated by the Zend script engine | Warning |
E_USER_ERROR |
User-generated fatal error via trigger_error() |
Fatal |
E_USER_WARNING |
User-generated warning via trigger_error() |
Warning |
E_USER_NOTICE |
User-generated notice via trigger_error() |
Notice |
E_RECOVERABLE_ERROR |
Catchable fatal error | Fatal (Catchable) |
E_DEPRECATED |
Runtime notices about deprecated features | Warning |
E_USER_DEPRECATED |
User-generated deprecation notice | Warning |
E_ALL |
Bitmask covering all supported error levels | Aggregate |
File Upload Error Codes
When processing $_FILES['upload']['error'], compare against these constants instead of magic numbers:
| Constant | Code | Description |
|---|---|---|
UPLOAD_ERR_OK |
0 |
File uploaded successfully |
UPLOAD_ERR_INI_SIZE |
1 |
File exceeds upload_max_filesize in php.ini |
UPLOAD_ERR_FORM_SIZE |
2 |
File exceeds MAX_FILE_SIZE specified in HTML form |
UPLOAD_ERR_PARTIAL |
3 |
File was only partially uploaded |
UPLOAD_ERR_NO_FILE |
4 |
No file was selected for upload |
UPLOAD_ERR_NO_TMP_DIR |
6 |
Missing temporary upload folder on server |
UPLOAD_ERR_CANT_WRITE |
7 |
Failed to write file to server disk |
UPLOAD_ERR_EXTENSION |
8 |
A PHP extension stopped the file upload |
CLI Stream Constants
Available automatically in Command Line Interface (CLI) scripts:
| Constant | Description | Direction |
|---|---|---|
STDIN |
Open stream resource reading from standard input | Input |
STDOUT |
Open stream resource writing to standard output | Output |
STDERR |
Open stream resource writing to standard error | Output |
Summary
- Use
PHP_OS_FAMILYinstead of comparing against individualPHP_OSstrings for cross-platform compatibility. - Use
UPLOAD_ERR_*constants when handling file uploads for descriptive error checking. - Leverage magic constants (
__DIR__,__CLASS__,__METHOD__) for zero-overhead compile-time introspection.