When debugging in a PHP project, we often need to see which constants are currently defined, especially in large projects or environments where multiple third-party libraries are integrated. get_defined_constants() is a built-in function provided by PHP that can help us get all defined constants, including system default, extended loaded, and user-defined constants. This article will introduce how to use this function to build a simple and practical debugging dashboard to help developers quickly understand system status and configuration information.
get_defined_constants() returns an associative array where the key is the name of the constant and the value is the value of the constant. By passing in true parameters, we can classify these constants by module, for example:
print_r(get_defined_constants(true));
The output of this function will be grouped by module, such as Core, date, pcre, user, etc., which is very helpful in understanding the source of constants.
The goal of debugging dashboards is to visually display various information about the development and operation environment. Using get_defined_constants() , we can build an HTML page that lists all the currently defined constants, especially user-defined constants in the "user" module.
In addition, we can also build a comprehensive debugging interface with other functions such as phpinfo() , ini_get_all() , $_SERVER, etc., but this article will focus on the display of the constant part.
Here is a simple PHP script that generates debug dashboard pages and displays various constant information in tabular form:
<?php
// Get all defined constants,And classified by module
$constants = get_defined_constants(true);
// HTML Output header
echo '<!DOCTYPE html><html lang="zh"><head><meta charset="UTF-8">';
echo '<title>PHP Constant debug dashboard</title>';
echo '<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h2 { background-color: #f0f0f0; padding: 10px; }
table { width: 100%; border-collapse: collapse; margin-bottom: 30px; }
th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
th { background-color: #e0e0e0; }
</style></head><body>';
echo '<h1>PHP Constant debug dashboard</h1>';
// Iterate through each module
foreach ($constants as $category => $consts) {
echo "<h2>Module:{$category}</h2>";
echo '<table>';
echo '<tr><th>Constant name</th><th>value</th></tr>';
foreach ($consts as $name => $value) {
$displayValue = htmlspecialchars(var_export($value, true));
echo "<tr><td>{$name}</td><td>{$displayValue}</td></tr>";
}
echo '</table>';
}
echo '</body></html>';
Security : Displaying all constants in a public environment may reveal sensitive information (such as keys, paths, etc.). It is recommended that this page be enabled only in the development environment, or add an authentication mechanism.
Performance issues : Although get_defined_constants() itself has little impact on performance, in projects with a lot of constants, generating and rendering pages may cause browser stuttering. You can consider adding paging or expanding on demand.
To debug remote servers more flexibly, you can encapsulate the above page as a password protected debug portal, for example:
<?php
$token = $_GET['token'] ?? '';
if ($token !== 'your-secret-token') {
http_response_code(403);
exit('Access Denied');
}
// Then output the debug dashboard
By visiting https://debug.m66.net/dashboard.php?token=your-secret-token , you can view the constant status remotely. This method can also obtain a visual debugging experience without installing complex debugging tools.
get_defined_constants() is a powerful tool that is ignored by many developers. With simple packaging, you can build a fully functional and easy-to-use debug dashboard. It not only helps you understand the system's operating status, but also provides critical information when troubleshooting configuration issues. Debugging is no longer a blind man touching an elephant, but visually controllable. Integrating it into your development toolbox will definitely make debugging work more effective.