[php] PHP: trying to create a new line with "\n"

i'm writing this:

echo "foo";
echo "\n";
echo "bar";

and "bar" is not written in the line below.

What am i doing wrong?

Javi

This question is related to php newline

The answer is


This answer was so obvious and it took me forever to figure out:

echo "foo
bar";

I know that looks like it's wrapping. It's not. What I did is I literally hit return halfway through the string, between foo and bar. It creates a new line in the HTML source and makes your PHP look horrible. This was in Linux/Apache.


$a = 'John' ; <br/>
$b = 'Doe' ; <br/>
$c = $a.$b"&lt;br/>";

This works perfectly for me...

echo nl2br("\n");

Reference: http://www.w3schools.com/php/func_string_nl2br.asp

Hope it helps :)


PHP generates HTML. You may want:

echo "foo";
echo "<br />\n";
echo "bar";

if your text has newlines, use nl2br php function:

<?php
$string = "foo"."\n"."bar";
echo nl2br($string);
?>

This should look good in browser


the html element break line depend of it's white-space style property. in the most of the elements the default white-space is auto, which mean break line when the text come to the width of the element. if you want the text break by \n you have to give to the parent element the style: white space: pre-line, which will read the \n and break the line, or white-space: pre which will also read \t etc. note: to write \n as break-line and not as a string , you have to use a double quoted string ("\n") if you not wanna use a white space, you always welcome to use the HTML Element for break line, which is <br/>


We can apply \n in php by using two type

  1. Using CSS

    body {
        white-space: pre-wrap;
    }
    

    Which tells the browser to preserve whitespace so that

    <body>
        <?php
            echo "Fo\n Pro";
        ?>
    </body>
    

    Result:

    Fo
    Pro

  2. Using nl2br

    nl2br: Inserts HTML line breaks before all newlines in a string

    <?php
        echo nl2br("Fo.\nPro.");
    ?>
    

    Result

    Fo.
    Pro.


Assuming you're viewing the output in a web browser you have at least two options:

  1. Surround your text block with <pre> statements

  2. Change your \n to an HTML <br> tag (<br/> will also do)


We can use \n as a new line in php.

Code Snippet :

 <?php
  echo"Fo\n";
  echo"Pro";
?>

Output:

Fo
Pro


If you want a new line character to be inserted into a plain text stream then you could use the OS independent global PHP_EOL

echo "foo";
echo PHP_EOL ;
echo "bar";

In HTML terms you would see a newline between foo and bar if you looked at the source code of the page.

ergo, it is useful if you are outputting say, a loop of values for a select box and you value having html source code which is "prettier" or easier to read for yourself later. e.g.

foreach( $dogs as $dog )
echo "<option>$dog</option>" . PHP_EOL ;

It will be written on a new line if you examine the source code of the page. If you want it to appear on a new line when it is rendered in the browser, you'll have use a <br /> tag instead.


Since it wasn't mentioned, you can also use the CSS white-space property

body{
    white-space:pre-wrap;
}

Which tells the browser to preserve whitespace so that

<body>
    <?php
        echo "hello\nthere";
    ?>
</body>

Would display

hello
there

echo "foo<br />bar";

If you want to write plain text, you must ensure the content type is set to Content-Type: text/plain. Example:

header('Content-Type: text/plain');

If you are dealing with HTML, you have two options. One is to inset a new line using <br> (Or <br /> for XHTML). The other is to put the plain text in a <pre> element (In this case "pre" stands for preformatted).