in reply to question on strings

Original question aside:

As for forcing $var to be a string, if it is it is. There's not really any coercion needed, but if you absolutely must then "$var" would force stringification (but it's usually not needed). Given the other weirdness the problem probably lies elsewhere; as was suggested above make sure that $var contains what you think it does through an extra print or in the debugger.