Andrew Danks Andrew Danks - 4 months ago 11
PHP Question

Using a keyword as variable name in an INI file

I have the following in an INI file:

[country]
SE = Sweden
NO = Norway
FI = Finland


However, when var_dump()ing PHP's parse_ini_file() function, I get the following output:

PHP Warning: syntax error, unexpected BOOL_FALSE in test.ini on line 2
in /Users/andrew/sandbox/test.php on line 1
bool(false)


It appears that "NO" is reserved. Is there any other way I can set a variable named "NO"?

Answer

Another hack would be to reverse your ini keys with their values and use array_flip:

<?php

$ini =
"
    [country]
    Sweden = 'SE'
    Norway = 'NO'
    Finland = 'FI'
";

$countries = parse_ini_string($ini, true);
$countries = array_flip($countries["country"]);
echo $countries["NO"];

Still you will need to use quotes around NO (at least), if you do

Norway = NO

you don't get an error but value for $countries["NO"] will be an empty string.

Comments