0

INI_SCANNER_TYPED, used as the 3rd argument to parse_ini_file(), should conserve boolean, null and integer values "when possible", according to the documentation

However, when constants are parsed, the values are converted to strings. The documentation is not clear on whether this is expected to happen. It is certainly undesirable, I can't imagine a single scenario where this would be useful. Other scanning types are to be used when you want string conversion, surely?

For example with the following ini file:

NUMBER = 1
TEXT = "1"
DEFINED_INTEGER_CONSTANT = FOO
DEFINED_BOOLEAN_CONSTANT = BAR
UNDEFINED_CONSTANT = BAZ

I use the following php file and get output in comments

<?php
declare(strict_types=1);
define("FOO", 123);
define("BAR", true);

$ini_file = parse_ini_file(__DIR__ . '/test.ini', false, INI_SCANNER_TYPED);

foreach($ini_file as $key => $value) {
    define($key, $value);
}

var_dump(NUMBER); // => int(1)
var_dump(TEXT); // => string(1) "1"
var_dump(DEFINED_INTEGER_CONSTANT); // => string(3) "123"
var_dump(DEFINED_BOOLEAN_CONSTANT); // => "1"
var_dump(UNDEFINED_CONSTANT); // => "BAZ"

I neither expect nor want the string conversion in the 3rd and 4th examples. Can I use parse_ini_file to parse constants without string conversion, or do I need to do this another way? Is this something to do with the "where possible" clause in the documentation? Is keeping the original types not possible here for some reason?

Tested in PHP 7.1.26 and 7.3.7.

Mark Fisher
  • 711
  • 8
  • 26

1 Answers1

0

There does not seem to currently be a way to get parse_ini_file to behave in the expected way.

I have reported this and it has been verified as a bug - see here.

Mark Fisher
  • 711
  • 8
  • 26