A little note about the new line character, Ajax and a silly mistake…

Just something “fun” to share on Friday…

I had a simple Ajax auto-complete type of widget in my app… which ultimately returned this view:

if(!empty($items)) {
     foreach($items as $key => $item) {
       echo $item . '\n';
     }
 }

Everything worked well, except that \n was not treated as a new line character. So, all items were returned as one long string, rather than being broken down into “individual” lines in the auto-complete widget.

After some wall and head connections, I’ve found out that instead of ‘\n’ I have to use double quotes “\n” to get this to work as expected. Problem solved.

Hopefully it’ll help someone out there :)

%d bloggers like this: