PHP

ADD LINE BREAKS & NEW LINES IN PHP

(simple examples)

DEFINE VARIABLE $lines = "Line 1 Line 2 Line 3";

DEFINE LINE BREAKS AS-IT-IS

01

ECHO OUTPUT echo "Line 1 Line 2 Line 3";

MAC USES \r, LINUX USES \n, WINDOWS USES \r\n

JUST USE \r\n REGARDLESS $lines = "Line1 \r\n Line 2";

LINE BREAK CHARACTERS

02

ONLY WORK WITH DOUBLE QUOTES $bad = 'Line1 \r\n Line 2';

PHP_EOL IS AUTOMATICALLY \r, \n, OR \r\n BASED ON THE OS.

END-OF-LINE CONSTANT

03

$users = ["Joe", "Jon", "Joy"]; echo implode($users, PHP_EOL);

HTML BREAK <BR> $lines = "Line 1<br>Line 2";

HTML LINE BREAKS

04

NEW LINE TO <BR> $lines = "Line 1 Line 2"; echo nl2br($lines);