是否可以使用 PHP 对 .ini 文件使用内联注释?

问题描述 投票:0回答:3

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

我更喜欢注释与变量内联、位于变量之后的系统。

有一些关于要使用的语法的问题吗?

php comments ini
3个回答
87
投票

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

key1=value
; this is a comment
key2=value ; this is a comment too

7
投票

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


3
投票
<?php
$ini = <<<INI
; this is comment
[section]
x = y
z = "1"
foo = "bar" ; comment here!
quux = xyzzy ; comment here also!
a = b # not a comment
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 # not a comment
        )

)

请注意,PHP 的 INI 解析器不支持

#
注释。

© www.soinside.com 2019 - 2024. All rights reserved.