• Skip to content
  • Skip to primary sidebar
  • Skip to footer

nSiteful Web Builders

Building a Better Web - One Site at a Time.

  • Home
  • About
    • Testimonials
  • Web Sites
  • Online Marketing
  • WordPress Support
    • Customized WordPress Training
  • Web Applications
  • Blog
    • Archive Listing Minimalistic
    • Blog Articles Grouped by Category
    • Case Studies
    • First Looks
    • General
    • Reviews
    • Snippets
    • Techniques
  • Contact Jeff
    • Purchase Retainer Consulting Hours

October 22, 2015 By Jeff Cohan Leave a Comment

Diagnostic PHP: Get All User Constants

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.

You might also enjoy...

Why, When, and How to use sprintf and printf What to look for in WordPress themes: Pluggability Diagnostic PHP: Get All User Functions Diagnostic PHP: Get All Included Files PHP OOP Virtual Study Group?
  • Choose the best match.

Filed Under: Snippets Tagged With: Diagnostics, php

  • Choose the best match.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

mailchimp signup

Subscribe to get notified when new articles are published. Unsubscribe any time. No spam. I promise. Check out my newsletter archives.

social

Twitter Facebook LinkedIn

Recent Articles

  • Automatic “Visit Web Site” Links on WordPress Portfolio Posts December 21, 2018
  • How to Reuse Content in WordPress with the amr shortcode any widget Plugin December 9, 2018
  • Dynamic Linear Gradients for Background Images in Genesis November 27, 2018
  • Dive Into WordPress Custom Post Types – Part 3 November 16, 2018
  • Add Dynamic Table of Contents to a Series of WordPress Posts November 11, 2018

Filter By Category/Tag

Categories

  • Case Studies (7)
  • First Looks (8)
  • General (52)
  • Reviews (3)
  • Snippets (12)
  • Techniques (28)

Popular Tags

Advanced Custom Fields Arrays Attachments Blogging Child Themes Content Marketing CSS Custom Fields Custom Post Types Diagnostics Email Embedding Facebook Genesis Horde HTML Images iPhone Libra Marketing Media MySQL NextGen Gallery PayPal Photo Gallery php Pinterest Plugins Post Formats Pricing Programming Project Management SEO Seth Godin Shortcodes Social Networking Surveys Tables Taxonomies Twitter Web design Web forms WordBooker WordPress YouTube

partners

StudioPress Premium WordPress Themes

siteground wp hosting

Web Hosting

Footer

Background

Web Sites | WordPress Support | Web Applications.

Formally trained in liberal arts and education (I have a B.A. in Government from Harvard and studied Secondary Education at Rutgers Graduate School), I have honed my skills in the communication arts and sciences as a teacher, trainer, instructional designer, writer, photographer, calligrapher, helpdesk manager, database programmer, and multimedia developer.

(I've also been a group counselor, waiter, bartender, bicycle messenger boy, computer salesman, carpenter's helper, financial analyst, and school board president.)

Tech

Systems since 1983.
Web sites since 1994.
PHP since 2001.
WordPress since 2007.

Contact

770-772-5134
Email Jeff

Copyright © 2019 · nSiteful Web Builders, Inc. · Log in