PHPで.iniファイルにインラインコメントを使用することは可能で安全ですか?
私は、コメントが変数の後に続くシステムを好みます。
使用する構文に関する落とし穴はありますか?
INI 形式では、セミコロンをコメント文字として使用します。ファイル内のどこでもそれらを受け入れます。
key1=value
; this is a comment
key2=value ; this is a comment too
組み込みの INI ファイル解析機能について話している場合、セミコロンは期待されるコメント文字であり、インラインで受け入れられると思います。
<?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
)
)