In PHP development, using constants is a very common practice. Constants are usually used to store some configuration or status values that will not be changed, such as the basic URL of the website, database connection parameters, version number, etc. A common way to define constants in PHP is to use the define() function, and if you want to view all defined constants (including custom and PHP predefined), you can use the get_defined_constants() function.
This article will explain how to use the define() and get_defined_constants() function in combination to view the definition and content of custom constants, and provide practical examples to help understand.
define() is a function used in PHP to define constants. Its basic syntax is as follows:
define(string $name, mixed $value, bool $case_insensitive = false): bool
$name : The name of the constant must be a legal identifier.
$value : The value of a constant, which can be a scalar (string, integer, floating point, boolean).
$case_insensitive : Whether it is case-insensitive, it has been deprecated since PHP 7.3.0.
define('BASE_URL', 'https://m66.net/');
define('SITE_NAME', 'My website');
The above code defines two constants, representing the basic address and name of the website respectively.
get_defined_constants() is used to get all defined constants and return as an associative array. This function can choose to organize results by classification to facilitate viewing which are user-defined and which are system default.
get_defined_constants(bool $categorize = false): array
$categorize : Whether to return the result by category. Set to true to return a multi-dimensional array.
print_r(get_defined_constants());
print_r(get_defined_constants(true));
When set to true , you will see an array output similar to the following structure:
Array
(
[Core] => Array
(
[E_ERROR] => 1
[E_WARNING] => 2
...
)
[user] => Array
(
[BASE_URL] => https://m66.net/
[SITE_NAME] => My website
)
)
Here is a complete example to illustrate how to define custom constants and use get_defined_constants(true) to view these constants:
<?php
// Define constants
define('BASE_URL', 'https://m66.net/');
define('SITE_NAME', 'My website');
define('VERSION', '1.0.0');
// Get all constants
$constants = get_defined_constants(true);
// Extract user-defined constants
$user_constants = $constants['user'] ?? [];
echo "自Define constants列表:\n";
foreach ($user_constants as $name => $value) {
echo "$name => $value\n";
}
自Define constants列表:
BASE_URL => https://m66.net/
SITE_NAME => My website
VERSION => 1.0.0
With the above method, you can easily view all custom constants and their current values, which is especially useful when debugging or viewing configurations.
Constants cannot be reassigned once defined.
It is recommended to use full capital naming to define constants to distinguish them from variables.
PHP built-in constants and extended-defined constants will be obtained by get_defined_constants() , so it is best to use the classification option and only view the user section.
The combination of define() and get_defined_constants() is a very practical tool in PHP. The former is used to define key configuration information, and the latter is used to debug and review the current constant state. Understanding and flexibly applying these two functions will help you build more maintainable and debugable PHP applications.