tags:

views:

258

answers:

4

Is it possible and safe to use inline comments for .ini files with PHP?

I prefer a system where the comments are inline with the variables, coming after them.

Are the some gotchas concerning the syntax to be used?

A: 

what ini files are you talking about and what inline comments? are you talking about php.ini file or your custom .ini file? if custom, why not to have custom format as well counting inline comments in?

dusoft
+2  A: 

If you're talking about the built-in INI file parsing function, semicolon is the comment character it expects, and I believe it accepts them inline.

Charles
What is the syntax limitations allowed on the right side of the = sign? Does it follow the usual string quoting and escaping syntax, such as matching '', "" and some of the usual regex escape characters?
I'm not sure. Why not try it and find out?
Charles
+3  A: 

INI format uses semicolon as a comment character. It accepts them anywhere in the file.

key1=value
; this is a comment
key2=value ; this is a comment too
n1313
A: 
<?php
$ini = <<<INI
; this is comment
[section]
x = y
z = "1"
foo = "bar" ; comment here!
quux = xyzzy ; comment here also!
a = b # comment too
INI;

$inifile = tempnam(dirname(__FILE__), 'ini-temp__');
file_put_contents($inifile, $ini);
$a = parse_ini_file($inifile, true);
if ($a !== false)
{
  print_r($a);
}
else
{
  echo "Couldn't read '$inifile'";
}

unlink($inifile);

Outputs:

Array
(
    [section] => Array
        (
            [x] => y
            [z] => 1
            [foo] => bar
            [quux] => xyzzy
            [a] => b # comment too
        )

)
raspi
I don't think you need to write to a temp file; `parse_ini_string()` will do the trick http://www.php.net/manual/en/function.parse-ini-string.php
Ken Keenan
parse_ini_string (PHP 5 >= 5.3.0)
raspi