Posts Tagged ‘charsets’

The magic empty string that is not empty

Wednesday, May 14th, 2008

I just helped one of our developers with a weird problem. The piece of code he was working on contained roughly this:

 
if ($str!='')
{
   echo "Hello $str";
}
 

He was importing a CSV file that didn't contain a value for $str, so $str was an empty string. So it should skip the echo statement according to the above code, right?

Wrong. The script echoed "Hello ". It completely did the $str!='' wrong.

In a debugger, we watched the value for $str and watched it step through the code. We clearly saw that while $str was empty (""), it executed the next line. Almost seems like a bug in the != operator but that obviously can't be the case.

This kind of baffled us.

To investigate, we var_dumped the value of $str, and this gave a very weird output:

 
   string(3) ""
 

An empty string, with a length of 3?

Eventually we noticed there were some weird control characters in there that translate to an empty string in output, but that do have a length. (Somehow this reminds me of black holes and dark matter).

We removed the char from the file (apparently, in a text editor, you could do 'delete' on the char, and although this didn't have a visible effect as it was an empty string in a text editor as well, it did work and we were able to process the file).

Lesson learned: not every empty string is an empty string. Control characters can have very weird effects. It can make a string look empty while it's not.

By the way: the people who created this file did so on a mac and uploaded it to a linux server. The weird chars were only at the beginning of the file, only on the first line. Does anybody know if there is some mac/linux/windows conversion that could cause these chars to appear?