php

How to add a new line within echo in PHP?

Line breaks are not automatically generated when outputting using echo():

<?php
    echo('A');
    echo('B');
    echo('C');
?>

Output:

ABC

Instead, it is necessary to set them manually. This is done using the notation “\n”, which means “new line”. The notation “\n” is automatically converted to a single new line character. Please note that this conversion is only performed by echo() if the string is surrounded by double quotes because single quotes do not parse the content (e.g. variables are not replaced automatically).

<?php
    // Line breaks can be created using \n.
    // \n must be in double quotes,
    // in single quotation marks it is ignored
    echo("This is a line break: \n");
    echo('This line break is ignored: \n');
    echo("(It's not a new line.)\n");
 
    // Line breaks can be placed in every string variable.
    // echo() is not required to print it out.
    $var = "A\nB\nC";
    var_dump($var);
?>

Output:

This is a line break: 
This line break is ignored: \n(It's not a new line.)
string(5) "A
B
C"
 
To be able to display line breaks when using single quotation marks, they must be surrounded by double quotation marks and linked to the first string:

<?php
    echo('That creates a new line:'."\n");
    echo('New line!');
?>

Output:

That creates a new line:
New line!
PHP MCQ - Multiple Choice Questions and AnswersPHP Questions and Answers – Object-Oriented OOP – Part 1This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Object-Oriented OOP”.   1. The concept of…Read More If you really want to output “\n” (i.e. a slash and then an n) instead of a new line, you can use single quotation marks, or you can escape the new line character:

<?php
    // using single quotes
    echo('\n');
    // using escaping, so the \n is preceded by an additional "\"
    echo("\\n");
?>

Output:

\n\n
 
Line breaks that are generated by \n are only displayed by browsers when the source text is opened. They are ignored when viewing the generated page. This is because the browsers try to display HTML by default and line breaks in HTML are ignored by “\n”. Instead there is a separate tag for creating line breaks, namely “<br>” (HTML4, HTML5) or “<br />” (XHTML):

<?php
    echo("(1) This line break is only displayed in the source code of a browser: \n");
    echo("(2) This line break is displayed by the browser, but does not create a line break in the source text: <br />");
    echo("(3) The following creates a line break in both cases:<br />\n");
?>

Output:

(1) This line break is only displayed in the source code of a browser: 
(2) This line break is displayed by the browser, but does not create a line break in the source text: <br />(3) The following creates a line break in both cases:<br />

Output by the browser:

(1) This line break is only displayed in the source code of a browser: (2) This line break is displayed by the browser, but does not create a line break in the source text: <br />
(3) The following creates a line break in both cases:<br />
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

One thought on “How to add a new line within echo in PHP?

  • Easiest way is to use nl2br:

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *