Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

comments

php

ini

People also ask

How do I comment a line in an INI file?

Comments in the INI must start with a semicolon (";") or a hash character ("#"), and run to the end of the line. A comment can be a line of its own, or it may follow a key/value pair (the "#" character and trailing comments are extensions of minIni).

Where should I put PHP INI?

user. ini file is the default configuration file for running applications that require PHP. It is used to control variables such as upload sizes, file timeouts, and resource limits. This file is located on your server in the /public_html folder.

What purpose does a .ini file serve?

An . INI file is a type of file that contains configuration information in a simple, predefined format. It is used by Windows OSs and Windows-based applications to store information about the user's preferences and operating environment.


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

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.


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

)