Oliver Nassar

PHP: Accessing constants in INI files

March 25, 2011

I use INI files heavily in my apps, generally to setup configuration files that are processed/run at runtime. They store really general, all-purpose data such as ini-values, paths to certain files, or error message controls.

When storing paths to other files though, I thought it'd be useful if I could reference constants, since paths are normally only useful in the context of other system/application paths.

The process was actually pretty simple. I simply prepended the constant (haven't tried appending) to the string value itself, like so:

[local]
display_errors=true
error_log=APP"/logs/php-error.log"
ignore_user_abort=true
log_errors=true

When I run the parse_ini_file function on this file, the error_log value will be calculated to have the APP PHP-constant prepended to it.

I'm not sure if there are any significant performance-issues with this, but it works well for what I need, and makes my code cleaner all in one :)