3

There is a thing make me puzzled in 2.2 of PSR-2 document.

enter image description here

All I know is that different os use different line ending as default :

  • windows:CRLF
  • unix:LF
  • mac:CR

I want to know why should "All PHP files MUST use the Unix LF(linefeed) line ending" . Or in other word, what will line ending cause ?

chenxinlong
  • 1,677
  • 2
  • 15
  • 30

3 Answers3

4

The main reason behind this is scv (Source Code Versioning), where changing line-endings cause unnecessary differences and conflicts.

Secondly, we usually serve our services on Linux, which is based on Unix, which use the LF file ending.

The first one causes the need of a Standard, and the second one tells you why you should use LF Standard.

windows:CRLF = '\r\n'
unix:LF      = '\n'
mac:CR       = '\r' // macOS also changed to LF a long time ago btw.

Update: also note that, whilst PHP - and imo. all the other scripting languages - doesn't really depend on a specific line-ending, in other languages this might cause issues.

balintant
  • 2,774
  • 1
  • 19
  • 33
3

PSR is made to resolve the coding style conflicts. EOL is a point of conflict, therefore it defined it in some way. The PSR working group voted in the question of line_endings as ?: 5, LF: 17. https://groups.google.com/forum/#!msg/php-fig/c-QVvnZdMQ0/TdDMdzKFpdIJ

Even if it doesn't cause any syntactical difference, in the PSR-2 standard it had been defined this way.

lintabá
  • 733
  • 9
  • 18
2

Using other line endings will still work in your PHP programs. But the purpose of that rule is stated in the beginning of that page (PSR-2):

The style rules herein are derived from commonalities among the various member projects. When various authors collaborate across multiple projects, it helps to have one set of guidelines to be used among all those projects. Thus, the benefit of this guide is not in the rules themselves, but in the sharing of those rules.

ByteSlinger
  • 1,439
  • 1
  • 17
  • 28
  • Thx. I got your point that PSR is for sharing a rule . But there is another question, which is what will cause if we use different line ending when programing ? Such as use CRLF in unix like systems or use LF in windows . Or even push both of it to one repository. – chenxinlong Jan 17 '18 at 07:52
  • 1
    As mentioned in the answer by @balintant, different line endings can cause some source code control systems to identify differences in the source code where there are none and create new source file versions simply because the line endings are different. PSR2 added this line ending rule to avoid that issue. – ByteSlinger Jan 17 '18 at 17:49