Creating a "two way" configuration file Creating a "two way" configuration file json json

Creating a "two way" configuration file


ini

I'd write ini files, myself. As you said, the syntax is very simple, and that's what you want in a config file. The ini format's "key+value" pairing is exactly what you'd get with a database—without the database.

Related SO you may have seen already: create ini file, write values in PHP

Plus you can use parse_ini_file() to read it.

XML

XML isn't all that bad. It may be more work to write it (and may not be as clear to the user as an ini file), but reading it is really easy.

Create it:

<?php// Create file$xml = new SimpleXMLElement( '<?xml version="1.0" ?><config></config>' );// Add stuff to it$xml->addChild( 'option1' );$xml->option1->addAttribute( 'first_name', 'billy' );$xml->option1->addAttribute( 'middle_name', 'bob' );$xml->option1->addAttribute( 'last_name', 'thornton' );$xml->addChild( 'option2' );$xml->option2->addAttribute( 'fav_dessert', 'cookies' );// Save$xml->asXML( 'config.xml' );?>

Read it:

<?php// Load$config = new SimpleXMLElement( file_get_contents( 'config.xml' ) );// Grab parts of option1foreach( $config->option1->attributes() as $var ){    echo $var.' ';}// Grab option2echo 'likes '.$config->option2['fav_dessert'];?>

Which gives you:

billy bob thornton likes cookies

Documentation for SimpleXML


I'd go with the ini. They're really not that hard to write. I personally hate XML. It's so bloated... even if the file size doesn't matter, it still makes me cringe at it's wordiness and the amount of typing I have to do. Plus, people are dumb. They won't close their tags.


The standard way would be XML files. They don't create that much overhead and are easily extensible. However, JSON files are the easiest on the programming end.

I'd rank my preference:

  1. XML
  2. JSON
  3. ini (last resort)