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:
User Constants: Array ( [ADD_HISTORY_PAGE] => /crm/add-history.php [ADMIN_INCLUDES_PATH] => /home/user/public_html/crm/_includes/ [CRM_ADD_HISTORY_PAGE] => /crm/add-history.php [CRM_EDIT_CONTACT_PAGE] => /crm/add-edit-contact.php [CRM_EDIT_HISTORY_PAGE] => /crm/edit-history.php [CRM_EDIT_RESULT_CODES_PAGE] => /crm/add-edit-result-code.php [HISTORY_REPORT_PAGE] => /crm/history-report.php [MODULE_INCLUDES_PATH] => /home/user/public_html/crm/_includes/ [MODULE_ROOT_DIR] => /crm/ [ROOT_DIR] => /crm/ [SHOW_DIAGS] => 1 [SITE_FOOTER] => /home/user/public_html/crm/_includes/pagebottom.php [SITE_HEADER] => /home/user/public_html/crm/_includes/pagetop.php [VIEW_HISTORY_PAGE] => /crm/view-history.php )
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.
Leave a Reply