Get User Defined Constants in PHP

Jul 15th, 2010

PHP function - get_defined_constants()  gives all the constants defined in current scope.  It can be CORE Constants, extensions defined constants and user defined constants. As a developer you may be interested in all defined constants or user defined constants at a time. As the total number of constants printed on screen can be very high, you may like to print the concerned constants at a time.

For user defined constants:

<?php
$const = get_defined_constants(true);
print_r($const['user']);
?>

For Core Constant, user 'Core' as keyword -
print_r($const['Core']).

For checking all values in the function, just print it as see what is stored within it.

The categorize parameter to get_defined_constants() was added in PHP 5.

 
Possibly Related posts:
  1. July 18th, 2010 at 04:02 | #1

    Do you know some of the reasons why you would need to have all of the constants ?

  2. July 18th, 2010 at 07:54 | #2

    Do not know. But you may need to know all user defined constants.
    Possibly among all kind of constants you can looks at extension constants
    coming into the scope!

  3. July 27th, 2010 at 23:15 | #3

    Your blog is very interesting!
    May I exchange links with you?
    I will come to visit your blog usually!

    • July 27th, 2010 at 23:17 | #4

      Currently I am very tired with my blog change. Something wrong has happened with my original theme.

  1. July 15th, 2010 at 09:26 | #1
Comments are closed. You are welcome to write on Facebook page.
blog comments powered by Disqus