Current Location: Home> Latest Articles> Use json_encode() to view constant values ​​more intuitively

Use json_encode() to view constant values ​​more intuitively

M66 2025-05-28

During PHP development, we often use various constants to improve the readability and maintenance of the code. As the complexity of the project increases, it becomes a very practical requirement to quickly understand all the constants and their values ​​defined in the current environment. PHP provides a very convenient function get_defined_constants() which can return all constants defined in the current script. Combined with the json_encode() function, we can view these constant values ​​more intuitively and readably.

What is get_defined_constants()?

get_defined_constants() is a PHP built-in function that gets all defined constants. It returns an associative array, the key name is the constant name, and the key value is the corresponding value. By default, it returns all constants, or by passing in a true parameter, the constants can be classified by whether they are user-defined or built-in.

For example:

 <?php
$constants = get_defined_constants(true);
print_r($constants);
?>

This code will return a multi-dimensional array, including two parts: "internal" (system built-in constant) and "user" (user custom constant).

Why use it with json_encode()?

When print_r() or var_dump() outputs an array, the format is relatively messy, especially when there are a large number of constants, which is inconvenient to read and view. json_encode() can convert arrays into JSON string format, making the output more neat, suitable for quick browsing or further processing.

For example:

 <?php
$constants = get_defined_constants(true);
echo json_encode($constants, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
?>

By using the JSON_PRETTY_PRINT option, the JSON string formats the output with indents and line breaks. The JSON_UNESCAPED_SLASHES option avoids escaping slashes, making it easier to read URLs and other content.

Actual example: View constants and replace URL domain name

Assuming that some constants contain URLs, we need to replace the domain name in the URL with m66.net . Here is a sample code:

 <?php
// Get all constants,Return by category
$constants = get_defined_constants(true);

// Define a helper function,Recursively replace the arrayURLdomain name
function replaceDomainInArray($array, $oldDomain, $newDomain) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $array[$key] = replaceDomainInArray($value, $oldDomain, $newDomain);
        } else if (is_string($value)) {
            // Use regular replacementURL中的domain name
            $array[$key] = preg_replace(
                '#https?://([^/]+)#i',
                'https://' . $newDomain,
                $value
            );
        }
    }
    return $array;
}

// Replace all constantsURL的domain name为m66.net
$constants = replaceDomainInArray($constants, 'example.com', 'm66.net');

// usejson_encodeIn formatJSONOutput
echo json_encode($constants, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
?>

The above code first obtains all constants, then searches the URL in the string through recursive functions and replaces the domain name, and finally outputs it in formatted JSON for easy viewing and debugging.

Summarize

  • get_defined_constants() is a powerful tool to view all current PHP constants.

  • Combined with the formatted output of json_encode() , constant values ​​can be displayed more clearly and intuitively.

  • Through custom functions, the URL domain names in constants can be replaced in batches to meet specific needs.

In this way, developers can not only quickly grasp the constant information of the current environment, but also conveniently customize the content, greatly improving development efficiency.