Home » Php » coding style – PHP: Is there a reason to put a newline at the end of file

coding style – PHP: Is there a reason to put a newline at the end of file

Posted by: admin April 23, 2020 Leave a comment


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?

How to&Answers:

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.


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!


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!


It is probably derived from the C standard: http://c0x.coding-guidelines.com/ (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 warningincludeing a file without new line at the end could cause similar problems like in C.


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.