how to create and get data from config files in php code example

Example: create config value file in php

<?php
$config = array(
    "database" => "test",
    "user"     => "testUser"
);

function writeConfig( $filename, $config ) {
    $fh = fopen($filename, "w");
    if (!is_resource($fh)) {
        return false;
    }
    foreach ($config as $key => $value) {
        fwrite($fh, sprintf("%s = %s\n", $key, $value));
    }
    fclose($fh);

    return true;
}

function readConfig( $filename ) {
    return parse_ini_file($filename, false, INI_SCANNER_NORMAL);
}

var_dump(writeConfig("test.ini", $config));
var_dump(readConfig("test.ini"));

Tags:

Php Example