Current Location: Home> Latest Articles> Overview of Commonly Used Predefined Constants in PHP

Overview of Commonly Used Predefined Constants in PHP

M66 2025-08-05

Overview of Commonly Used Predefined Constants in PHP

During PHP development, predefined constants offer convenient ways to retrieve details about the current runtime environment. These constants allow developers to access information like PHP version, OS type, execution interface, and error levels efficiently.

PHP_VERSION

This constant returns the current version of PHP. Example output:

echo PHP_VERSION; // Outputs something like 8.1.6

PHP_OS

Returns the name of the operating system PHP is running on. For example:

echo PHP_OS; // Outputs Linux, WINNT, Darwin, etc.

PHP_SAPI

This constant returns the Server API that PHP is using. Common values include cli (Command Line Interface), apache2handler (Apache module), and cgi-fcgi (FastCGI).

echo PHP_SAPI; // Outputs something like apache2handler

PHP_EOL

Returns the appropriate line ending string for the platform. Useful for writing cross-platform compatible code.

echo 'Line 1' . PHP_EOL . 'Line 2';

E_ALL

Represents a bitmask that includes all PHP error types. Useful for enabling comprehensive error reporting.

error_reporting(E_ALL);

E_ERROR

Represents fatal run-time errors. The script will terminate.

error_reporting(E_ERROR);

E_WARNING

Non-fatal run-time warnings. Execution will continue.

error_reporting(E_WARNING);

E_NOTICE

Indicates notices that the script encountered something that could be an error, but execution is not affected.

error_reporting(E_NOTICE);

E_USER_ERROR

Represents user-generated error messages. Treated as fatal errors.

trigger_error("Custom error", E_USER_ERROR);

E_USER_WARNING

User-generated warning messages, useful for debugging.

trigger_error("Custom warning", E_USER_WARNING);

E_USER_NOTICE

User-generated notices. Typically used for informative messages.

trigger_error("Custom notice", E_USER_NOTICE);

Summary

The predefined constants described above are very useful in PHP development and debugging. Mastering them can significantly improve the robustness and maintainability of your code.