Current Location: Home> Latest Articles> Differences of get_defined_constants() in different PHP versions

Differences of get_defined_constants() in different PHP versions

M66 2025-05-22

get_defined_constants() is a built-in function in PHP to get all defined constants. It returns an associative array where the key is the constant name and the value is the constant value. This is useful when debugging, reflecting, or analyzing the runtime environment. However, in different versions of PHP, the behavior of this function may show some differences, mainly reflected in the following aspects:

1. The number and type of constants change

With the development of PHP, new core constants are constantly being introduced, especially after adding new extensions or refactoring existing modules. For example, in PHP 7.x and PHP 8.x, changes in the error handling mechanism resulted in the introduction of a large number of new E_* error level constants.

Example:

 print_r(array_keys(get_defined_constants()));

In PHP 5.6, you may not see merge constants like E_ERROR | E_RECOVERABLE_ERROR , but after PHP 7, there will be many more definitions of such combined values.

2. Behavior changes of classification parameters ( get_defined_constants(true) )

Starting from PHP 5.3, get_defined_constants() supports a Boolean parameter $categorize , and when set to true , constants are classified by extension.

Example:

 print_r(get_defined_constants(true));

In different PHP versions, the classification names of different extension modules may change. For example:

  • Arrays in PHP 7.2 may contain Core , pcre , SPL , date , libxml , openssl , curl , etc.

  • Such as ffi , sodium , and fiber introduced after PHP 8.0 will also be added to the output results.

3. Differences between compilation and operation environments

Whether certain constants appear in the results of get_defined_constants() may be affected by configuration files such as php.ini or compile options. For example, whether Zend OPcache is enabled, whether specific extension modules are loaded, etc.

Sample code (running in the CLI):

 define('MY_APP_VERSION', '1.0.0');
print_r(get_defined_constants());

This output is often inconsistent between the development server and the production server, because the extension and configuration parameters they enable may be different.

4. Newly introduced language structure and built-in constants

PHP 8 introduces syntax features such as JIT, match expressions, attributes, etc., and also brings new constants accordingly. Running get_defined_constants() will find the occurrence of constants such as PHP_FD_SETSIZE , T_MATCH , T_ATTRIBUTE , etc.

These new constants do not exist at all in the old version, so if you rely on these constants when writing highly compatible code, you must use the defined() function to judge.

5. Change or delete of constant values

In rare cases, some defined constants are changed or discarded in a new version. While the PHP team usually ensures backward compatibility, it is not ruled out to make adjustments when major version upgrades are upgraded. For example, some extensions that interact with the underlying system may change the default value or delete certain constants that are no longer supported because they rely on updates to the C library.