CR vs LF vs CRLF

If the above title seems cryptic, it’s supposed to be. It’s representative of a problem that I came across today when I was editing one of the plugins on this site. When I went to check the plugin status, I was shocked to see that instead of the normal options to activate/ deactivate the plugin, all I got was a mish-mash of PHP code.

I couldn’t think why this had happened, then it hit me. I had done the editing on my MacBook. The Mac, just like every other platform has built-in text editors. These editors treat text in much the same way as editors on other platforms, although there is one crucial difference. When you hit the enter key on a Mac, a special character is inserted to signal that a new line should be started. On a Mac, this is called the Carriage Return, or CR, character. On a Windows editor, the enter key inserts a Carriage Return – Line Feed, (CR-LF), character, and on a Linux machine, it’s a Line Feed, (LF), character.

So each of the major platforms treat a new line in a different manner. The upshot is that if you write a text document on a Windows machine, the new line will be interpreted correctly on both a Linux and Mac, as it uses characters common to both. However, if you write a text document on a Mac, then it won’t be interpreted correctly on a Linux machine, and vice versa.

So here was my problem, I had edited the PHP files on my Mac and uploaded the changed files to a Linux server. Using EditPad Lite for Windows, I was able to easily convert the newline characters from CR to CR-LF, and re-upload the file. Problem solved. The only thing that I can’t figure out is that if Linux has a problem with the CR-only newline character, how come the plugin worked OK?