PHP parse_ini_file „NO“
September 3rd, 2010
During work wih language files I got the problems using the php method parse_ini_file
.
Imagine you have a language file this – lang.en.ini :
MAYBE="maybe" NO="no" SURE="sure" YES="yes"
To create a string table you’ll write some code like this:
$stringTable = parse_ini_file('lang.en.ini'); // debug output var_dump($stringTable);
But running the script will produce the following error:
Warning: Error parsing in.ini on line 2 in /tmp/pi.php on line 3 Call Stack: 0.0003 57396 1. {main}() /tmp/pi.php:0 0.0003 57428 2. parse_ini_file() /tmp/pi.php:3 array(1) { ["MAYBE"]=> string(5) "maybe" }
Whats wrong? It seems so, that parse_ini_file
has problems with parsing the ‚NO‘ as it stops the program execution at this point. So I had a look to the PHP function reference for parse_ini_file
but there is nothing said according to forbidden identifiers. Hmmm.
So I had to dig trough this problem and find the solution in the head comment of the php.ini file:
…
; The syntax of the file is extremely simple. Whitespace and Lines
; beginning with a semicolon are silently ignored (as you probably guessed).
; Section headers (e.g. [Foo]) are also silently ignored, even though
; they might mean something in the future.
;
; Directives are specified using the following syntax:
; directive = value
; Directive names are *case sensitive* – foo=bar is different from FOO=bar.
;
; The value can be a string, a number, a PHP constant (e.g. E_ALL or M_PI), one
; of the INI constants (On, Off, True, False, Yes, No and None) or an expression
; (e.g. E_ALL & ~E_NOTICE), or a quoted string („foo“).
…
… the value can be …, one of the INI constants (On, Off, True, False, Yes, No and None). Aha! So for me it seems that all of the ini constant aren’t valid names for ini values.
Leave a Reply