We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Config.ini numeric value to string issue - stripping leading zeros

So in my config.ini I have a value like value = "0001" however when I call that value in my php code the leading zeros get stripped because it is being enterpreted as an integer. Trying to cast it as a string in php does not work, I guess because Phalcon has already cast it as an integer before it gets to my code. I need a way to preserve the leading zeros by keeping it as a string. How can I do this?

The only workaround I have found is a really bad one - I added a non integer character infront of the value, then I have to strip that character in my code. I need a sensible fix for this!

your problem is here look how to invalidate is_numeric function like add a char or something

good luck

@emiliodeg well it seems that's exactly what @al35mm is already doing.

IMHO this comment in Phalcon source is simply a fraction of bad design: We have to cast values manually because parse_ini_file() has a poor implementation. really? Additional orchestration of this kind just caused trouble for someone (OP) where one would least expect.

parse_ini_file() won't do such a thing, as it will adhere to basics - if you put '0x0a' it shall be string (literal) and not a number, whilst 0x0a would indeed be converted to 10 and thus processed as integer.



20.4k

@emiliodeg well it seems that's exactly what @al35mm is already doing.

IMHO this comment in Phalcon source is simply a fraction of bad design: We have to cast values manually because parse_ini_file() has a poor implementation. really? Additional orchestration of this kind just caused trouble for someone (OP) where one would least expect.

parse_ini_file() won't do such a thing, as it will adhere to basics - if you put '0x0a' it shall be string (literal) and not a number, whilst 0x0a would indeed be converted to 10 and thus processed as integer.

That is exactly right. I have had to prepend a string character to the leading zeros to force it to be passed as a string then strip that character out when called. That is a pretty vile bit of programming, but I don't seem to have any choice. I guess the issue is that the config.ini passer is conforming to the ini specification. It may be good if we could cast the type by prepending (string) to a value or cast it as a string by wrapping it in quotes which would adhear to the php specification but then break the ini specification.