Last updated May 9th, 2024 at 03:02 pm
Sometimes I want to see all the constants I have defined. Here is some diagnostic PHP I use for displaying all user-defined constants.
function diag_defined_user_constants( $sort = true, $echo = true ) {
$constants = get_defined_constants(true);
$u_constants = $constants['user'];
if ( $sort ) {
ksort($u_constants);
}
if ( $echo ) {
echo 'User Constants:<pre>' . print_r($u_constants, true) . '</pre>';
} else {
return $u_constants;
}
}
The native PHP function get_defined_constants returns an associative array of all defined constants, including user-defined constants and those defined in PHP core and by its extensions. By including the optional categorize parameter and setting it to true, the function returns a multidimensional array whose first-dimension keys are the categories.
Since I care only about user-defined constants, line 3 defines the variable $u_constants accordingly. Note the use of the ksort function here, since the keys of the $constants['user'] array are associative.
I would call the function like this:
diag_defined_user_constants(); // using default arguments
I get a result that looks like this (with values hidden):
User Constants: Array ( [ADD_HISTORY_PAGE] => [value_goes_here] [ADMIN_INCLUDES_PATH] => [value_goes_here] [CRM_ADD_HISTORY_PAGE] => [value_goes_here] [CRM_EDIT_CONTACT_PAGE] => [value_goes_here] [CRM_EDIT_HISTORY_PAGE] => [value_goes_here] [CRM_EDIT_RESULT_CODES_PAGE] => [value_goes_here] [HISTORY_REPORT_PAGE] => [value_goes_here] [MODULE_INCLUDES_PATH] => [value_goes_here] [MODULE_ROOT_DIR] => [value_goes_here] [ROOT_DIR] => [value_goes_here] [SHOW_DIAGS] => [value_goes_here] [SITE_FOOTER] => [value_goes_here] [SITE_HEADER] => [value_goes_here] [VIEW_HISTORY_PAGE] => [value_goes_here] )
If you have questions or comments about this snippet of diagnostic PHP — or if you have some of your own diagnostic PHP to share — feel free to leave a comment.
Like what you see? Share with others and join my mailing list. No long-term commitment, unsubscribe any time.
Leave a Reply