Use get_cfg_var() when you need to returns a PHP configuration value from the original configuration file in a PHP application. The examples below show the actual operation and its limitations.
What Is get_cfg_var() in PHP?
Returns a PHP configuration value from the original configuration file. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
get_cfg_var(string $option): array|string|false
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
var_dump(get_cfg_var("memory_limit"));
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
var_dump(get_cfg_var("display_errors"));
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
$settingName = "upload_max_filesize"; $configured = get_cfg_var($settingName); echo $configured === false ? "Not in config" : (string)$configured;
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
It reads configuration-file values rather than necessarily current runtime overrides.
Comparison With Related Functions
| Approach | Difference |
|---|---|
get_cfg_var() | Returns a PHP configuration value from the original configuration file. |
| Related operation | ini_get() reads the current effective setting; get_cfg_var() reads configuration file data. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | It reads configuration-file values rather than necessarily current runtime overrides. |
| Confusing this function with a related operation | ini_get() reads the current effective setting; get_cfg_var() reads configuration file data. |
Key Takeaways
- Returns a PHP configuration value from the original configuration file.
- It reads configuration-file values rather than necessarily current runtime overrides.
- ini_get() reads the current effective setting; get_cfg_var() reads configuration file data.
Use get_cfg_var() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.