Get a list of all available constants with PHP
Posted July 17th, 2008 in PHP
PHP has the function get_defined_constants() which allows you to get a list of all the available constants in an array. This post looks at how to use the get_defined_constants() function and example output from it.
The get_defined_constants() function takes one optional parameter, which specifies whether or not to categorise the constants. The default is false, which means an associative array is returned containing the constant name as the key and the constant value as the value. If the categorise parameter is set to true, a multi-dimensional array is returned containing the section names as the index and then a sub array of name-value pairs in the second dimension.
For example:
print_r(get_defined_constants());
will output something like this:
Array
(
[E_ERROR] => 1
[E_WARNING] => 2
[E_PARSE] => 4
[E_NOTICE] => 8
[E_STRICT] => 2048
[E_CORE_ERROR] => 16
[E_CORE_WARNING] => 32
[E_COMPILE_ERROR] => 64
[E_COMPILE_WARNING] => 128
[E_USER_ERROR] => 256
[E_USER_WARNING] => 512
[E_USER_NOTICE] => 1024
[E_ALL] => 2047
[TRUE] => 1
[FALSE] =>
[NULL] =>
[ZEND_THREAD_SAFE] =>
[PHP_VERSION] => 5.1.6
[PHP_OS] => Linux
[PHP_SAPI] => apache2handler
[DEFAULT_INCLUDE_PATH] => .:/usr/share/pear
...
and
print_r(get_defined_constants(true));
will output something like this:
Array
(
[internal] => Array
(
[E_ERROR] => 1
[E_WARNING] => 2
...
)
[libxml] => Array
(
[LIBXML_VERSION] => 20626
[LIBXML_DOTTED_VERSION] => 2.6.26
...
)
[xml] => Array
(
[XML_ERROR_NONE] => 0
[XML_ERROR_NO_MEMORY] => 1
...
)
...
)
If you have any user defined constants they will be listed at the end when not categorised, and in a [user] section when categorised.
Related posts:
- Get the value of a PHP constant dynamically (Monday, January 18th 2010)
- How to tell if a PHP constant has been defined already (Monday, February 16th 2009)
- PHP Date Constants (Saturday, August 16th 2008)
- Get a list of all available classes with PHP (Thursday, July 10th 2008)
- Get a list of all available functions with PHP (Thursday, July 3rd 2008)
- PHP Magic Constants (Saturday, December 8th 2007)
Share or Bookmark
Share or Bookmark this page using the following services. You will need to have an account with the selected service in order to post links or bookmark this page.
Subscribe or Follow
Subscribe via RSS or email, or follow me on Facebook or Twitter below. The RSS icon takes you through to Feedburner where you can select the service or application to use.
