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 に答える