In PHP development, constants are a very important part of them. They are used to store unchangeable values for easy reuse in code. PHP provides a very practical function get_defined_constants() to get all defined constants in the current script. This article will introduce in detail the usage of the get_defined_constants() function and parse things to pay attention to when using it.
get_defined_constants() is a built-in function in PHP, which is used to return all currently defined constant arrays. It not only contains user-defined constants, but also contains various system constants built into PHP.
Function definition:
array get_defined_constants([bool $categorize = false])
Parameter $categorize (optional): The default value is false , which means that a one-dimensional array is returned, containing all constants and their values.
If true is passed, a two-dimensional array is returned, and the constants are stored in different categories.
Here is a simple example showing how to get all constants using get_defined_constants() :
<?php
// Get all constants,Not classified
$constants = get_defined_constants();
print_r($constants);
?>
The output is an associative array containing the constant name and constant value, for example:
Array
(
[PHP_VERSION] => 8.0.3
[E_ERROR] => 1
[E_WARNING] => 2
...
)
If you want to see different types of constants more clearly, you can pass in the true parameter:
<?php
// Get all constants,And classify
$constants = get_defined_constants(true);
print_r($constants);
?>
The output structure is similar to:
Array
(
[Core] => Array
(
[E_ERROR] => 1
[E_WARNING] => 2
...
)
[date] => Array
(
[DATE_ATOM] => Y-m-d\TH:i:sP
[DATE_COOKIE] => l, d-M-Y H:i:s T
...
)
[user] => Array
(
[MY_CONST] => 123
)
)
This way you can easily see the classification of constants.
Debug program : Quickly view all available constants in the current system and program.
Dynamically generate code : implement code adaptation or configuration by reading system constants.
Framework development : All built-in or user-defined constants need to be listed in the framework for unified management.
Return to the array larger <br> If a large number of constants are defined in the program, the returned array will be relatively large. It is recommended to use it during debugging to avoid printing directly in the production environment.
Read-only nature <br> The value of a constant cannot be changed. The array returned by get_defined_constants() is for viewing only, and constants cannot be modified through this function.
Use of classification parameters <br> The array returned by classification when passing true is helpful for understanding the system constant structure, but if you only care about the tiled list of all constants, you can pass false or not pass parameters.
Cooperation with other constant functions <br> Constants can be dynamically defined in combination with the define() function, and get_defined_constants() can detect custom constants.
<?php
// Define custom constants
define('MY_CONST', 'Hello World');
define('ANOTHER_CONST', 2025);
// Get all constants并分类
$allConstants = get_defined_constants(true);
// Output user-defined constant parts
print_r($allConstants['user']);
?>
Output result:
Array
(
[MY_CONST] => Hello World
[ANOTHER_CONST] => 2025
)
This way, custom constants can be easily managed and viewed.
get_defined_constants() is a very useful built-in function in PHP, which can help developers quickly obtain all current constant information. Whether it is a system constant or a user-defined constant, it can be seen at a glance. Reasonable use of this function can greatly facilitate debugging and code management, but you need to pay attention to the return array size and the use of classification parameters.