Skip to content
Advertisement

PHP parse_ini_file TC_CONSTANT warning

I’ve got a weird issue, weird as in nothing comes up on Google. I’m trying to parse an ini file full of HTTP status codes, StatusCodes.ini. I’ve tested in three different environments, locally (WAMP) on a shared host (Hostmonster.com) and now on a dedicated machine running CentOS w/ CPanel/WHM. The first two environments seem to work fine, however on the dedicated machine I’m getting a warning:

Warning:  syntax error, unexpected TC_CONSTANT in StatusCodes.ini on line 8

when running:

$ini = parse_ini_file('StatusCodes.ini',true);
$codes = $ini['codes'];

the ini file looks like:

[codes]
100 = Continue
101 = Switching Protocols
200 = OK
201 = Created
202 = Accepted
203 = Non-Authoritative Information
204 = No Content
205 = Reset Content
206 = Partial Content
300 = Multiple Choices
301 = Moved Permanently
302 = Found
303 = See Other
304 = Not Modified
305 = Use Proxy
307 = Temporary Redirect
400 = Bad Request
...

In the case you don’t want to count, 204 = No Content, is line 8. I’ve taken the line out and nothing changes. Any suggestions?

Advertisement

Answer

The problem, as you noted, is with the line 204 = No Content.

This is because of the No which is a special value within the INI file (along with others). The INI parser gets to this line and reads the No value for the 204 key, then finds the trailing <space>Content text which generates the error.

The PHP manual notes (e.g. on the page for parse_ini_file):

Values null, no and false results in "", yes and true results in "1".

The simple fix would be to enclose all of the values in double-quotes, or at the very lease those which start with the INI keywords, like:

[codes]
100 = "Continue"
101 = "Switching Protocols"
200 = "OK"
201 = "Created"
202 = "Accepted"
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement