35

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?

random
  • 9,324
  • 10
  • 63
  • 77
  • 2
    your question is not clear. in order to get help your questions must be understandable, no body is in your head. – Mohamed Sep 12 '09 at 08:29

3 Answers3

75

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
  • 18,981
  • 7
  • 27
  • 45
6

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
  • 48,924
  • 13
  • 96
  • 136
  • 1
    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? –  Sep 12 '09 at 09:24
  • I'm not sure. Why not try it and find out? – Charles Sep 12 '09 at 19:32
2
<?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
  • 5,464
  • 2
  • 30
  • 45