PHP encoding standard: Which to use?

The truth is that there is no default encoding to PHP, however, we can use some standards already used by famous frameworks. If it works for them, it will work for you!

If you often work with projects where other developers also put his hand in the cookie jar, probably already may have noticed that each one works with a default PHP encoding type. This is normal in any kind of service, not only in development, however, is extremely important to establish the pattern since the beginning of your project before it’s too late.

By default, I’m referring to how the codes are written, for example: Use tabs or spaces? Which mode of linebreaks (EOL)? A tab is equivalent to how many spaces?

They may seem stupid questions to some, but I’ve had many problems with different encoding modes in the same project.

I’ll explain a fact that occurred to me just below.

Based on a true story
Some time ago, I worked in a small project where we were three developers. All in the same room, next to each other, each taking care of a part of the development.

There was no way anything go wrong, we were in line and in constant communication.

It was then that the default encoding has betrayed us.

I was using Linux (to be more precise, Ubuntu), the other two developers were using Windows and Mac respectively. Each with a different text editor.

My standard of EOL-End-of-line (end of the line, in Portuguese) was the or \n (default UNIX), the developer was using Mac OS too, though, of another developer who was using Windows was or \r\n (Windows Standard).

See sample images below:
eol-unix

Without seeing all the characters (as in the images above), this can go unnoticed, however, believe it or not, he’s going to be trouble later.

The Problem
The fact is that when we the files onto the main server (which had CentOS), both my files as the developer who used Mac OS, functioned perfectly, since those who were with the default EOL Windows did not work at all.

After losing a lot of time trying to understand the problem, I decided to connect to the server via SSH to see how the files were being read, and guess what? The line breaks in the EOL format Windows doesn’t work.

On PC everything was normal but when the files were sent to the server, they simply had no line break.

With that, the excerpt below:

Was read as follows: