42

When I commit a php file to github I get the message "No newline at end of file".

It's just a warning and I remember, that for any reason it is good to have a newline at the end of a file.

But why? Is it a remnant of long gone times, does it still have advantages or is it even required in php? If yes, for what reason?

nulltoken
  • 64,429
  • 20
  • 138
  • 130
Christian Kolb
  • 1,368
  • 2
  • 23
  • 43

6 Answers6

30

It's not required by PHP, but it can cause problems with some diff tools. The warning is a just a warning and can be ignored if desired, but I would recommend following convention and having the last character be a newline.

jmoreno
  • 12,752
  • 4
  • 60
  • 91
27

This is a PSR-2 convention that states:

All PHP files MUST end with a single blank line.

Why? Because utilities that are supposed to operate on files (like the diff utility) may not cope well with lines that don't end with a newline; this is how POSIX states in one of its rules:

3.206 Line

A sequence of zero or more non- <newline> characters plus a terminating character.

Therefore, lines not ending in a newline character aren't considered actual lines. Which means: if your last line is a line of code (instead of an empty line), it might be ignored by those tools and can cause your program to break!

bdsl
  • 288
  • 2
  • 9
numediaweb
  • 16,362
  • 12
  • 74
  • 110
9

Now, in PSR-12 #2.2 Files:

All PHP files MUST use the Unix LF (linefeed) line ending only. All PHP files MUST end with a non-blank line, terminated with a single LF.

Buu Pham
  • 131
  • 1
  • 4
6

The warning is to help you to detect a possibly defective, truncated file, on the assumption that file without a newline at the end is suspect for being truncated.

Other than that, the only reason to avoid source files without a terminating newline is to avoid the warning!

Eli Rosencruft
  • 7,232
  • 1
  • 19
  • 17
  • Usefull to know: Truncated files can occur for example when data or a data stream (such as a file) is stored in a location too short to hold its entire length. – Robin Bastiaan Oct 10 '22 at 12:39
4

It is probably derived from the C standard: http://c0x.coding-guidelines.com/5.1.1.2.html (paragraph 123). Reasons include that some compilers or other text text processing tools process the source code line by line thus also the last source line has to end with a new-line character.

Also see this: "No newline at end of file" compiler warning - includeing a file without new line at the end could cause similar problems like in C.

Community
  • 1
  • 1
MrTJ
  • 13,064
  • 4
  • 41
  • 63
2

Another reason to add new lines everywhere is git history. Let's say you don't add an empty newline in the end of the file, at first the "Git blame" says it is you who wrote that last line.

If somebody else now wants to add a new line in the end. He MUST change your last line, there is no way around. He has to add a \n and then add his new code... So his name is now on your line only to add a simple \n

git says 2 lines changed by adding only one

Julesezaar
  • 2,658
  • 1
  • 21
  • 21
  • Very clear and useful example why you should want to always write a newline at the end of a file, altho this is true independent of php. – Robin Bastiaan Oct 10 '22 at 12:35