Predefined Constants
Last updated onPHP has a number of predefined constants that give you some information about the language and its environment. You can call them through your PHP script code.
These constants are specific values that don’t change and are just designed to be called during the code execution process.
PHP itself includes something called the Zend Engine and several other modules that help communicate with web servers; its name is SAPI modules.
Why Use Predefined Constants?
Predefined constants of PHP are just like your assistant. They hold major keys, the name of a file, its path, number of line, or even configuration data of PHP. Well, if you need to refer to something technical without providing all details, then in that case, PHP has got your back. Using pre-defined constants not only saves you a great deal of time but makes your code much neat and tidy.
Plus, it cuts down on the possibility of mistakes that can pop up when you try to hard-code the values all over the place. Instead of cluttered setup with numbers and strings strewn all over your code, the constants give you clarity-a bit of peace of mind.
We can categorize them into groups based on their functionalities and purposes; let’s begin with that.
Categories of PHP Predefined Constants
There’s a handful of categories when it comes to these constants. Each one has its own job, so let’s break them down to make it clear what they’re for and how they can actually help you out.
- Core Constants: These are built right into PHP, covering the basics. They let you check things like PHP’s current version or the maximum integer size. For example,
gives you the version of PHP your server is running—no guesswork needed.PHP_VERSION
- Magic Constants: Despite the name, they aren’t quite magical. They’re just special constants that change based on where you are in your code.
, for instance, gives you the full path and filename of the script. Every time you use it, it knows which file you’re in and serves up the right information. There’s also__FILE__
, which, unsurprisingly, tells you the line number where it’s used. Magic constants are like these helpful markers, telling you exactly where things are happening in your code.__LINE__
- Error Reporting Constants: PHP can throw a lot of errors, and error reporting constants help you manage them without pulling your hair out. Constants like
orE_WARNING
let you control what kind of errors get reported, so you can fine-tune your code’s error-handling process. Using them can save you from a lot of unnecessary panic by filtering out errors that aren’t worth worrying about while highlighting those that are.E_NOTICE
- PHP_INI Constants: These constants let you control your PHP configuration at runtime. Constants like
,INI_ALL
, andINI_USER
specify who has control over specific settings, which is helpful when you’re working with user-specific configurations or system-level tweaks.INI_SYSTEM
- Predefined Interface Constants: When working with interfaces, some constants help set things like default configurations or flags. You might not encounter these unless you’re working deeply with PHP’s built-in interfaces, but they’re there for when you need them.
Anyway, let's start with each one in details.
PHP Version Information
These constants are used to retrieve information about the current PHP version that is installed on your machine. The most common one is the PHP_VERSION constant. Let’s see an example of it:
if ( version_compare(PHP_VERSION, '8.0.30') !== 0) {
echo "This version is not 8.0.30";
}
In the following paragraphs, you will see all constants of PHP Version Information with a brief explanation.
- PHP_VERSION to retrieve the current installed version of PHP.
- PHP_MAJOR_VERSION to capture the current major version number of the PHP interpreter.
- PHP_MINOR_VERSION to display the minor version of the PHP interpreter.
- PHP_RELEASE_VERSION to show the current release version of PHP.
- PHP_VERSION_ID to merge major, minor, and release versions into a numeric representation.
- PHP_EXTRA_VERSION includes additional version details, such as pre-release or patch information.
- PHP_ZTS checks if PHP functions properly in situations where multiple things are occurring at the same time.
- PHP_DEBUG tells us if PHP has a special feature for finding and fixing problems in our code.
The following image shows you all of these constants in one shot.

Anyway, let’s move to the predefined constants of system information in PHP
System Information
The constants here are used to get information about the operating system, which appears as an environment of the current PHP installer. Let’s see each one in details:
- PHP_OS: Represents the operating system PHP is running on.
- PHP_OS_FAMILY: Classifies the OS family, such as Windows, Unix, or macOS.
- PHP_SAPI: Identifies the Server API, indicating how PHP interacts with web servers.
- PHP_EOL: Represents the end-of-line sequence used in PHP scripts.
- PHP_INT_MAX: Maximum value for integers in PHP.
- PHP_INT_MIN: Minimum value for integers in PHP.
- PHP_INT_SIZE: Size of an integer in bytes.
- PHP_FLOAT_DIG: Decimal digits of precision for floating-point numbers.
- PHP_FLOAT_EPSILON: Smallest representable positive floating-point number.
- PHP_FLOAT_MIN: Minimum floating-point number representable.
- PHP_FLOAT_MAX: Maximum floating-point number representable.
- DEFAULT_INCLUDE_PATH: Default search paths for include files.
- PEAR_INSTALL_DIR: Directory where PEAR packages are installed.
- PEAR_EXTENSION_DIR: Directory for PEAR extensions.
- PHP_EXTENSION_DIR: Directory for PHP extensions.
- PHP_PREFIX: Common prefix for PHP installations.
- PHP_BINDIR: Directory containing PHP binary.
- PHP_BINARY: Path to the PHP binary.
- PHP_MANDIR: Directory containing manual pages.
- PHP_LIBDIR: Directory for PHP libraries.
- PHP_DATADIR: Directory for read-only data used by PHP.
- PHP_SYSCONFDIR: Directory for system-wide configuration files.
- PHP_LOCALSTATEDIR: Directory for modifiable runtime data.
- PHP_CONFIG_FILE_PATH: Path to the PHP configuration file.
- PHP_CONFIG_FILE_SCAN_DIR: Directory for additional ini files.
- PHP_SHLIB_SUFFIX: Shared library suffix (e.g., “.so” on Unix).
- PHP_FD_SETSIZE: Maximum number of file descriptors in a set.
Let’s move into the following part, which comprises the error handling constants.
Error Handling
These constants are used to control how errors are reported and handled in your scripts. Let’s see them in the list below.
- E_ERROR to E_USER_DEPRECATED: Numerical representations of PHP error levels, ranging from critical errors (E_ERROR) to user-triggered deprecations (E_USER_DEPRECATED).
- E_ALL: Represents all error levels, indicating that all types of errors should be reported.
- E_STRICT: A level emphasizing strict adherence to coding standards, promoting compatibility.
- COMPILER_HALT_OFFSET: Special offset value for
__halt_compiler()
, indicating the end of file for embedded data.
Let’s move into the section below, which covers Boolean and Null Values constants.
Boolean and Null Values
These are predefined words in the PHP core used to handle logical operations.
- true
- false
- null
Windows-specific Constants
PHP_WINDOWS_EVENT_CTRL_C (int): An integer constant in PHP representing the Ctrl+C (SIGINT) event on Windows. Used for handling interrupt signals, typically to gracefully terminate a process or handle user interruptions.
PHP_WINDOWS_EVENT_CTRL_BREAK (int): An integer constant in PHP representing the Ctrl+Break event on Windows. This is another signal event that can be used for special handling in a PHP script, allowing developers to respond to specific user inputs or system signals.
Let’s summarize it
Wrapping up
A set of predefined constants in PHP sheds light on the language and environment. All these are easily accessible from your code, yet static for different functionalities.
We looked at PHP Version Information, System Information, and Error Handling, dividing the constants into their respective uses. The tour covered how PHP interacts with web servers, operating system information, and error control.
We have discussed Boolean and Null Values and even took a sneak peek at some Windows-specific constants. These are just but a few of the precious tools developers have to allow for adaptability in code and pave the way for solving problems efficiently. While the landscape may change and is constantly evolving around us, PHP constants literally stand as pillars to bring stability and functionality to our coding efforts.
Thank you for reading. Happy Coding!
Frequently Asked Questions (FAQs)
What is a PHP predefined constant?
Why should I use predefined constants instead of hard-coding values?
What are magic constants in PHP?
How can I check my PHP version using a predefined constant?
What are error reporting constants, and how do they work?
What does `PHP_INT_MAX` do, and when would I use it?
How do I use `DIRECTORY_SEPARATOR` in PHP, and why is it helpful?
Can I create my own predefined constants in PHP?
What is `__DIR__`, and how is it different from `__FILE__`?
What are PHP_INI constants, and when would I use them?
How can I use `__LINE__` for debugging in PHP?
What does `E_ALL` do, and should I always use it?
What’s the difference between `E_WARNING` and `E_NOTICE`?
Is it possible to change the value of a predefined constant?
What happens if I try to redefine a predefined constant?
Did you find this tutorial useful?
Your feedback helps us improve our tutorials.