PHP Echo Line Breaks PHP Echo Line Breaks php php

PHP Echo Line Breaks


Use the PHP_EOL constant, which is automatically set to the correct line break for the operating system that the PHP script is running on.

Note that this constant is declared since PHP 5.0.2.

<?php    echo "Line 1" . PHP_EOL . "Line 2";?>

For backwards compatibility:

if (!defined('PHP_EOL')) {    switch (strtoupper(substr(PHP_OS, 0, 3))) {        // Windows        case 'WIN':            define('PHP_EOL', "\r\n");            break;        // Mac        case 'DAR':            define('PHP_EOL', "\r");            break;        // Unix        default:            define('PHP_EOL', "\n");    }}


  • \n is a Linux/Unix line break.
  • \r is a classic Mac OS (non-OS X) line break. Mac OS X uses the above unix \n.
  • \r\n is a Windows line break.

I usually just use \n on our Linux systems and most Windows apps deal with it ok anyway.


Jarod's answer contains the correct usage of \r \n on various OS's. Here's some history:

  • \r, or the ASCII character with decimal code 13, is named CR after "carriage return".
  • \n, or the ASCII character with decimal code 10, is named "newline", or LF after "line feed".

The terminology "carriage return" and "line feed" dates back to when teletypes were used instead of terminals with monitor and keyboard. With respect to teletypes or typewriters, "carriage return" meant moving the cursor and returning to the first column of text, while "line feed" meant rotating the roller to get onto the following line. At that time the distinction made sense. Today the combinations \n, \r, \r\n to represent the end of a line of text are completely arbitrary.