45

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,774
  • 10
  • 66
  • 83
  • 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

85

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
  • 20,555
  • 7
  • 31
  • 46
  • Do you have any reference to the official documentation of the use as `;` besides as starting character? – PhoneixS Dec 17 '21 at 09:28
  • 1
    While this question is about PHP, the Windows API does not support inline comments for INI files. I'm not sure whether PHP for Windows will use the Windows API to read INI files. In any case, Wikipedia is not a the best source. – Thomas Weller Dec 22 '21 at 09:29
7

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
  • 50,943
  • 13
  • 104
  • 142
  • 2
    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
3
<?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,962
  • 3
  • 34
  • 51