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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1414625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:28:31  来源:igfitidea点击:

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

phpcommentsini

提问by n1313

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

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

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?

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

回答by n1313

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

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

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

回答by Charles

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.

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

回答by raspi

<?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
        )

)