Writing ini files in PHP

For reading ini files, you can use PHP's internal parse_ini_file function.

For reading configuration files like LoxBerry's general.cfg with parse_ini_file, we recommend to use the scanner mode INI_SCANNER_RAW as PHP may get problems trying to parse LoxBerry's settings. 

For writing ini files with PHP you can use the included PHP library Config/Lite. It is included in Loxberry Core.

Initializing

If you would like to use it, load it via 

require_once "Config/Lite.php";

Usage

The usage is very simple. To load an ini file, use 

 

$cfg = new Config_Lite("$lbpconfigdir/myconfig.cfg",LOCK_EX,INI_SCANNER_RAW);

INI_SCANNER_RAW should be used for read of different spells of boolean values. Otherwise booleans could be empty especially while writing the ini file.

If you wouldn't like to quote your values, disable quoting by

 

$cfg->setQuoteStrings(False);

The LOCK_EX parameter above locks the file for writing.

Access values from the array

If your ini file is loaded, you have access to an array of your values:

$myvalue=$cfg['section']['value'];

Set values

You can set values to your ini file by writing to the array:

$cfg['section']['value'] = $myvalue;

Slightly more complicated are boolean values like "on/off true/false". Most times the result will be wrong. For booleans there is an function getBool. More about the use of the included functions follows below.

 The LoxBerry module LBSystem (loxberry_system.php) includes functions, to translate fuzzy words like enabled, on, to the state True. See LBSystem::is_enabled($text) and LBSystem::is_disabled($text).

Object orientated access

Another way to read/write are OO style functions

To read a value use

 

$myvalue=$cfg->get("section","value");

To set a value use

 

$cfg->set("section","value",$myvalue);

Booleans

For booleans there is es special function called getBool. This function returns a real bool and translates all possible values to it

 

$mybool = $cfg->getBool("section","value",default);


You can also test if a value exists.

 

$exist = $cfg->has("section","value");

The return value is either True or False.

Save your changes

To save you values use

 

$cfg->save();

Documentation