PHP: Is there a reason to put a newline at the end of file PHP: Is there a reason to put a newline at the end of file php php

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


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!