\n vs. PHP_EOL vs. ?

DOS, Unix, and Mac (pre-OS X and OS X) all use different characters or character combinations to represent “go to the next line.”

  • DOS – Uses a CR+LF (that’s ASCII 13 followed by an ASCII 10, or \r\n) to represent a new line.

  • Unix – Uses an LF (that’s ASCII 10, or \n) to represent a new line.

  • Mac (pre-OS X) – Uses a CR (that’s ASCII 13, or \r) to represent a new line.

  • Mac (OS X) – Like Unix, uses an LF to represent a new line.

Therefore, when to use each one depends on what you’re going for. If you’re writing for a specific platform without the intention of portability, use the character or character combination to break lines that matter to that platform. The purpose of PHP_EOL is to automatically choose the correct character for the platform, so that your new lines are platform-independent.

All of these appear as a single space within a browser as browsers collapse whitespace into a display space for display purposes (unless you’re using <pre> as you mentioned, or CSS that changes the behavior of whitespace). This is where <br> comes in, as you’ve mentioned, which will convert these \n new line characters into <br> so that they provide line breaks in HTML display.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)