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

nSiteful Web Builders

Building a Better Web - One Site at a Time.

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

By Jeff Cohan, October 22, 2015

Diagnostic PHP: Get All User Constants

Last updated April 10th, 2020 at 12:30 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.

Related Posts

  1. Diagnostic PHP: Get All User Functions
  2. Diagnostic PHP: Get All Included Files
  3. Sort Multidimensional Arrays with PHP array_multisort
  4. Get From Foreigner (utility function)
  5. How to use Advanced Custom Fields with Genesis

Written by Jeff Cohan · Categorized: Snippets · Tagged: Diagnostics, php

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

  • Use Case for Custom Post Type: “In The News” March 10, 2023
  • Create a Custom Shortcode to Display a MemberPress Membership Price ANYWHERE on Your Website February 5, 2023
  • Avoid Direct Styling; Use CSS Instead September 21, 2022
  • Blog Tags: What They Are (and What They’re Not) August 5, 2022
  • How to Create a Simple Custom Events Plugin May 24, 2022

Filter By Category/Tag

Categories

  • Case Studies (7)
  • General (61)
  • Portfolio (5)
  • Reviews (12)
  • Snippets (16)
  • Techniques (38)

Popular Tags

Advanced Custom Fields Blogging Child Themes Content Marketing CSS CSS Grid Customer Service Custom Fields Custom Post Types Diagnostics Facebook FooGallery Genesis Gutenberg HTML Images iPhone Libra Live Chat Marketing Media MemberPress MemberPress Courses mu-plugins MySQL Photo Gallery php Pinterest Plugins Post Formats Pricing Project Management Security SEO Seth Godin Shortcodes Social Networking Surveys Taxonomies Trello Twitter Video Web design Web forms WordPress

siteground wp hosting

Web Hosting

wp101

EasyWordPresstutorialvideosforbeginners.
MemberPress CTA

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
Send Money
All Ways

Copyright 2023, nSiteful Web Builders, Inc.

 

Subscribe

Pardon the interruption. I know popups can be annoying. But I’d love to have you as a subscriber.

Sign up to be notified when new articles are published. Unsubscribe any time.

* indicates required

Powered by MailChimp

×