Diagnostic PHP: Get All User Constants

Share this!

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 ) {
	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:

	[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.

Share this!

About JeffCohan.com

The nSiteful Tech Blog (the official blog of nSiteful Web Builders, Inc. since January of 2013) is where I (Jeff Cohan) and (occasionally) associates will be posting articles of potential interest to like-minded techies, nSiteful clients who are playing active roles in the maintenance of their own Web sites and blogs, and pretty much anyone interested in how Web strategies and tools can help them reach their goals.

This entry was posted in Snippets and tagged , , by Jeff Cohan. Bookmark the permalink.

About Jeff Cohan

Jeff and his wife, Margie, are the proud parents of Sarah and Jake. Jeff is the founder, president and chief cook and bottle washer of nSiteful Web Builders, Inc., a Web development and Internet Consulting firm. In his spare time, Jeff builds Web sites and Web applications, plays guitar, putters around in his basement woodworking shop, mercilessly spoils his grandchildren, and creates videos from more than two decades of home movies. His current video project is an extended montage of people (mainly family members) asking him to stop filming them.

Leave a Reply

Your email address will not be published. Required fields are marked *