39

使用 PHP 对 .ini 文件使用内联注释是否可行且安全?

我更喜欢一个系统,其中注释与变量内联,紧随其后。

关于要使用的语法有一些陷阱吗?

4

3 回答 3

77

INI 格式使用分号作为注释字符。它在文件中的任何位置接受它们。

key1=value
; this is a comment
key2=value ; this is a comment too
于 2009-09-12T09:16:42.913 回答
6

如果您谈论的是内置的 INI 文件解析功能,分号是它所期望的注释字符,我相信它可以内联接受它们。

于 2009-09-12T08:34:26.680 回答
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);

输出:

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

)
于 2009-09-12T13:44:45.927 回答