Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What will line ending cause when programming?

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 ?

like image 858
chenxinlong Avatar asked Aug 01 '26 09:08

chenxinlong


2 Answers

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.

like image 63
balintant Avatar answered Aug 03 '26 21:08

balintant


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.

like image 34
lintabá Avatar answered Aug 03 '26 21:08

lintabá



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!