Current Location: Home> Latest Articles> How to output all constant values ​​using get_defined_constants function and display in optimal format?

How to output all constant values ​​using get_defined_constants function and display in optimal format?

M66 2025-05-18

In PHP, a constant is an identifier that will not be modified and is usually used to hold values ​​that remain unchanged during the run of the program. For debugging or learning purposes, we sometimes need to view all the constants defined in the current script. Fortunately, PHP provides a built-in function get_defined_constants() that can get information about all available constants at once.

This article will introduce how to use the get_defined_constants() function and explain how to output constant values ​​in a clear and organized format to improve readability and practicality.

1. Function introduction

get_defined_constants([bool $categorize = false]): array

This function returns an array containing all defined constant names and their corresponding values.

  • If no argument is passed (or false), a flat array is returned with the constant name as the key and the value is the constant value.

  • If true is passed, the returned 2D array will be classified according to the "module" of the constant, such as Core , pcre , user , etc.

2. Output all constants

Here is a simple example showing how to get all constants and print them in a tabular form:

 <?php
$constants = get_defined_constants(true);

// Only user-defined constants
$userConstants = $constants['user'];

echo "<table border='1' cellpadding='5' cellspacing='0'>";
echo "<thead><tr><th>Constant name</th><th>Constant value</th></tr></thead><tbody>";

foreach ($userConstants as $name => $value) {
    echo "<tr><td>{$name}</td><td>" . htmlspecialchars(print_r($value, true)) . "</td></tr>";
}

echo "</tbody></table>";
?>

In this example, we only extract the constants under the user category, that is, constants manually defined by the programmer in the script through the define() or const keywords. The output content is a standard HTML table for easy viewing on web pages.

3. Format all classification constants

If you want to view constants under all categories and display the results in groups, you can use the following code:

 <?php
$constants = get_defined_constants(true);

foreach ($constants as $category => $constGroup) {
    echo "<h2>Classification:{$category}</h2>";
    echo "<table border='1' cellpadding='5' cellspacing='0'>";
    echo "<thead><tr><th>Constant name</th><th>Constant value</th></tr></thead><tbody>";

    foreach ($constGroup as $name => $value) {
        echo "<tr><td>{$name}</td><td>" . htmlspecialchars(print_r($value, true)) . "</td></tr>";
    }

    echo "</tbody></table><br>";
}
?>

This way, each constant category is displayed separately, such as Core, pcre, user, curl and other modules, which is very suitable for a deep understanding of what predefined constants are in the PHP operating environment.

4. Export constant information to JSON

If you want to export constants in machine-readable form for easy use in other systems such as JavaScript or logging systems, you can convert them to JSON:

 <?php
header('Content-Type: application/json');
echo json_encode(get_defined_constants(true), JSON_PRETTY_PRINT);
?>

This way the output content structure is clear and suitable for remote debugging or system integration.

5. Combined with URL output to remote log server

Assuming you want to send user-defined constant values ​​to a remote server for logging (for example http://m66.net/log.php), you can do this:

 <?php
$userConstants = get_defined_constants(true)['user'];
$data = json_encode($userConstants);

// Send to remote server
$url = "http://m66.net/log.php";
$options = [
    'http' => [
        'method'  => 'POST',
        'header'  => "Content-Type: application/json\r\n",
        'content' => $data,
    ],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
?>

This method can be used for debugging tools, API tracking, or deploying verification scripts.

6. Summary

get_defined_constants() is a powerful function that not only helps us quickly obtain all constants defined by the system and user, but also outputs and processes them in various formats. Whether it is web display, logging, or remote call, flexible use in combination with actual scenarios can greatly improve our development efficiency and system observability.

Mastering this function is equivalent to opening a window in the PHP runtime world.