Use get_defined_constants() when you need to lists constants known to the current PHP runtime in a PHP application. The examples below show the actual operation and its limitations.
What Is get_defined_constants() in PHP?
Lists constants known to the current PHP runtime. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
get_defined_constants(bool $categorize = ...): array
Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.
Example 1: Basic Usage
<?php
$constants = get_defined_constants(); echo isset($constants["PHP_VERSION"]) ? "Found" : "Missing";
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Example 2: Different Input or Behavior
<?php
$groups = get_defined_constants(true); echo is_array($groups) ? "Grouped" : "Unavailable";
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Practical Example: Applying the Function
<?php
$runtimeConstants = get_defined_constants(); foreach (["PHP_INT_MAX","DIRECTORY_SEPARATOR"] as $name) echo $name . ": " . $runtimeConstants[$name] . PHP_EOL;
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Important Behavior and Edge Cases
The full list can be large and varies by installed extensions; do not expose it publicly.
Comparison With Related Functions
| Approach | Difference |
|---|---|
get_defined_constants() | Lists constants known to the current PHP runtime. |
| Related operation | defined() checks one name; get_defined_constants() enumerates them all. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | The full list can be large and varies by installed extensions; do not expose it publicly. |
| Confusing this function with a related operation | defined() checks one name; get_defined_constants() enumerates them all. |
Key Takeaways
- Lists constants known to the current PHP runtime.
- The full list can be large and varies by installed extensions; do not expose it publicly.
- defined() checks one name; get_defined_constants() enumerates them all.
Use get_defined_constants() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.