Current Location: Home> Latest Articles> Why do some constants not appear in the result?

Why do some constants not appear in the result?

M66 2025-06-06

In PHP programming, get_defined_constants() is a very useful function that returns an array of all defined constants in the current script. Normally, developers can use this function to view defined constants and their values. However, during actual use, you may find that some constants do not appear in the returned array, especially some system constants or built-in constants. This article will discuss why this happens and how to solve this problem.

1. Basic usage of get_defined_constants()

The get_defined_constants() function returns an array containing all the constants defined in the current script. The keys of this array are the names of constants, and the values ​​are the values ​​corresponding to the constants. For example:

 <?php
print_r(get_defined_constants());
?>

This code will output all defined constants in the current script. You will see some built-in constants, such as PHP_VERSION , PHP_OS , etc., but sometimes you will find that some constants are not appearing in the result.

2. Why do some constants not appear in get_defined_constants() ?

2.1 Definition position of constant

get_defined_constants() will only return constants defined in the current script context. If constants are defined in a specific scope, such as within a function or class, they will not appear in the global constant array. This is because the scope of these constants is limited to the scope in which they are defined.

For example, if you define a constant inside a function:

 <?php
function define_in_function() {
    define('MY_CONSTANT', 'Hello, World!');
}

define_in_function();
print_r(get_defined_constants());
?>

You will find that MY_CONSTANT will not appear in the returned array because it is defined inside the function, get_defined_constants() returns only constants defined in the global scope.

2.2 System Constants and Built-in Constants

Some system constants or PHP built-in constants may not appear in the return result of get_defined_constants() . For example, some of the built-in constants in PHP, such as PHP_VERSION and PHP_OS , may not appear in the returned constant array. The reason is that these constants are defined internally by PHP and they do not fall within the category of "constant definition" of the current script.

To confirm this, you can use get_constant directly to get the value of the built-in constant:

 <?php
echo PHP_VERSION;
echo PHP_OS;
?>

These constants are available directly in the PHP environment and will not appear in the result of get_defined_constants() .

2.3 Loading time for constants

If a constant is defined in a PHP extension or library, a specific extension or library may be loaded before it can be accessed in the script. For example, when you use some third-party libraries, constants defined in the library may only appear in get_defined_constants() after the library is introduced.

Suppose you use a PHP library called my_library and that will define some constants after loading. If you call get_defined_constants() before the library is loaded, you won't see these constants. Instead, after loading the library, call the function and you will be able to see these constants.

 <?php
// Assume this is a library file
include 'my_library.php';

print_r(get_defined_constants());
?>

2.4 Constant cache mechanism

Some constants may be dynamically defined by specific extensions or modules, which are not loaded immediately upon PHP startup, but are dynamically loaded through certain mechanisms during execution. Therefore, these constants may not be displayed immediately in get_defined_constants() . This situation may occur in PHP extensions such as curl or other related modules.

3. Best practices for using get_defined_constants()

To ensure you can correctly obtain all the constants you care about, the following are recommended:

  • Check the global scope : Make sure that when you call get_defined_constants() , the constants are defined in the global scope.

  • Check the load order : If your constants depend on a library or extension, make sure that when you call get_defined_constants() , all required files have been loaded correctly.

  • Dynamic Constants : For dynamically loaded constants, you may need to call get_defined_constants() at a specific time in the script, or get their values ​​through other means.

4. Use get_defined_constants() to process URLs

In PHP, if you use constants to handle URLs, especially when it comes to domain names, ensuring domain names consistency is also very important. Suppose your code has the following constant definition:

 <?php
define('API_URL', 'https://api.example.com/v1');
?>

When you want to replace the domain name m66.net , you can use the following method:

 <?php
define('API_URL', 'https://api.m66.net/v1');
?>

Make sure that the new domain name is correctly referenced in all places in the code where API_URL is used.

in conclusion

get_defined_constants() is a very useful function, but it does not always return all the constants you expect, especially for constants that are loaded dynamically within a specific scope or by PHP extensions. Understanding the scope of constants, loading order and defining timing can help you better use this function and ensure that you can get all the constant information correctly. When processing constants related to URLs, remember to update the domain name as required and ensure code consistency.