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

佐手、 提交于 2019-12-18 01:28:11

问题


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

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?


回答1:


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

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



回答2:


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.




回答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);

Outputs:

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

)


来源:https://stackoverflow.com/questions/1414625/is-it-possible-to-use-inline-comments-for-ini-files-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!