PHP get_defined_constants() Function

What is PHP get_defined_constants() Function?

If you want to get names and values of all the constants created with various PHP extensions and with define() function, use get_defined_constants() function. You can also get constants of a specific extension or define() function.

Syntax:

get_defined_constants(categorize)

Parameters:

The function has one parameter (optional)-

categorize (Optional): This parameter can accept two values-

  • FALSE – It is the default value. It returns a one-dimensional array with constant name as key and constant value as key’s value. Check example 1.
  • TRUE – It returns a multi-dimensional array with category as key of the outer array (first dimension) and constant and its value as key and values respectively of the inner array (second dimension). Check example 2.

Syntax for a single category:

get_defined_constants(categorize)[category_name]

Parameters:

The function has one extra parameter –

  • category_name – It returns all the constant names and their values of the “category_name” category. Check example 3.

Return Values:

The function returns an array of constant names and constant values optionally grouped by extension name.

Examples:

Example 1:

<pre>
<?php
define("SITENAME", "Schools Of Web");
print_r(get_defined_constants(FALSE));
?>
</pre>

Output:

Array
(
    [E_ERROR] => 1
    [E_RECOVERABLE_ERROR] => 4096
    [E_WARNING] => 2
          ………
   [SITENAME] => Schools Of Web
)

Explanation:

The get_defined_constants(FALSE) or get_defined_constants() displays all the constants created by PHP extensions (Here we display few of the many constants) at first then constant(SITENAME) defined by the define() function.

Example 2:

<pre>
<?php
define("SITENAME", "Schools Of Web");
print_r(get_defined_constants(TRUE));
?>
</pre>

Output:

Array
(   
[Core] => Array
        (
            [E_ERROR] => 1
            [E_RECOVERABLE_ERROR] => 4096
            [E_WARNING] => 2
            ………
        )
         ………
    [user] => Array
        (
            [SITENAME] => Schools Of Web
        )
 )

Explanation:

The TRUE parameter of the get_defined_constants(TRUE) function displays the multi-dimensional array with keys of the first-dimension (ex. “Core”) as the name of the PHP extension and the constants type (“user”) defined by the defines() function. And, in the second dimension, it displays constants names and their values.

Example 3:

<pre>
<?php
define("SITENAME", "Schools Of Web");
define("SITEURL", "https://SchoolsOfWeb.com");
print_r(get_defined_constants(TRUE)["user"]);
?>
</pre>

Output:

Array(
    [SITENAME] => Schools Of Web
    [SITEURL] => https://SchoolsOfWeb.com
)

Explanation:

Here, the get_defined_constants() returns all the constant of “user” category which were defined by the define() function.

Practical Usages of get_defined_constants() Function:

To get list of all the defined constants in your web app, you an use this function.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP get_defined_constants() Function

Sometimes you need to know all the defined constants in your app and this function the solution in that time. It is a built-in PHP option/info function.

Reference:

https://www.php.net/manual/en/function.get-defined-constants.php