article.categories

article.rights

Creative Commons License
This work is licenced under a Creative Commons Licence.

Article image for: Seemingly random escape sequence

Seemingly random escape sequence

By Dave Redfern (Writer)

Published: 18 Nov 15:25 in Web Development

Tags: framework

While creating a cache file on a Windows system I ran into a weird issue where the cache file was being corrupted when included.


While updating a cache mechanism, I was coming up with some issues. The cache was constantly being re-written even when the data did not appear to have changed. The cache file itself is pretty simple, just a pre-built PHP array of key => value pairs - except the value is the full file path on the file system. Now for some people this might start to make sense - especially Windows developers, but I could not figure out what was happening.

I started by echoing out the data that was being written and assigned, and moved through all the methods involved. I even checked the cache file itself, but all seemed to be in order.

It was only when performing a print_r on the returned cache file data that I finally realised that there was something a miss.

A new line was being inserted into one of the paths. You see I had used double quotes around my values and unknown to me at the time, \v is an escape sequence. Yup; \v. I guess it should make sense really as you can have \t \n etc. so really the path should only ever have been stored in single quotes but then I am used to running on Linux not Windows.

Replacing the cache creating code to output everything in single quotes fixed the problem.

The point to take home from this?

Always check how you are storing your string data and if it contains Windows path information, be sure that it is either escaped (\\) or use single quotes around it.

<  1  >