This question already has answers here:
new lines in PHP. how?
(3 answers)
how to write new lines in CLI and web browser?
(5 answers)
PHP - how to create a newline character?
(15 answers)
Closed 2 years ago.
Sample PHP code
wolf#linux:~$ cat 1-new-line.php
<?php
echo "Hello
World
Foo Bar!";
$aString = '
First Line
Second Line
Third Line';
echo $aString;
?>
wolf#linux:~$
It runs as expected in CLI
wolf#linux:~$ php 1-new-line.php
Hello
World
Foo Bar!
First Line
Second Line
Third Line
wolf#linux:~$
But not via browser
What's wrong in this code? How to render it properly in web browser?
Browsers interpret PHP's output as HTML, and in HTML newlines get replaced by spaces.
If you want to learn how to line-break in a browser, use <br /> or wrap sentences in <p> tags.
You can also tell a browser to treat your output as text, and not HTML. In that case, use the following header (before any output):
header('Content-Type: text/plain');
You are rendering it in a browser without any HTML formatting. Wrapping it in a <pre></pre> tags will preserve its plaintext formatting (newlines and whitespace) when rendered in the browser.
<pre>
<?php
echo "Hello
World
Foo Bar!";
</pre>
$aString = '
First Line
Second Line
Third Line';
<pre>
echo $aString;
</pre>
?>
Related
This question already has answers here:
\n vs. PHP_EOL vs. <br>?
(7 answers)
Closed 8 years ago.
I was studying php with tutorial. I recognized that in php is sometimes use like this
echo $myString."<br />"
Suddenly, there came another in the tutorial "\n" and I got confused. Can I use it just like I use
<br />
?
So like this?
echo $mySting."\n"
Is it like a html tag?
What's the difference between
<br /> and \n?
<br /> is a HTML line-break, whereas \n is a newline character in the source code.
In other words, <br /> will make a new line when you view the page as rendered HTML, whereas \n will make a new line when you view the source code.
Alternatively, if you're outputting to a console rather than somewhere that will be rendered by a web browser then \n will create a newline in the console output.
\n is a new line feed within the context of plain text, while <br /> is line break within the context of HTML.
HTML can interpret \n when using preformatted blocks (e.g. <pre>), but it does not by default, and should not unless there is a specific use case (like when quoting, citing poetry, or showing code).
<br /> should never be used to separate text that should otherwise be treated as a paragraph, heading or other group of text.
\n is a new line character (a literal new line as you would type in your code).
$newline = "
";
var_dump($newline === "\n");
// true
You can also use the PHP constant PHP_EOL (end of line). Note, that '\n' will render \n as a literal string; you must use double quotes to have it rendered as a new line character. Also note, that my above example may actually output false..since sometimes new lines are rendered as \r\n.
<br /> is an HTML element for a line break. This will show up as a new line when HTML is rendered in a browser.
The only time that \n will show up as a rendered line break in HTML, is when it is within a <pre> (pre-formatted text) element. Otherwise it would be the same as just formatting/indenting your HTML code:
<?php
echo "<html>\n\t<body>\n\t\tHello World!\n\t</body>\n</html>";
Outputs:
<html>
<body>
Hello World!
</body>
</html>
This question already has answers here:
Calling PHP functions within HEREDOC strings
(17 answers)
Use a variable within heredoc in PHP
(1 answer)
Closed 8 years ago.
I have been stuck trying to write PHP code inside some HTML tags I set up.
I have a variable inside a .php file called $content, and I have a HTML page code inside this variable in the following way:
$content = <<<HTML some html code in here HTML;
Now, within that HTML tag I wanted to add some PHP code, tried using <?php ..... ?>, however I wasn't successful with it. Can anyone help?
That's a HEREDOC syntax , The closing HTML; should be in margin else it wont work.
Valid Syntax
<?php
$somevar="Hello World";
echo <<<HTML
<b> Hey this is some text and I am adding this variable $somevar</b>
HTML;
Invalid Syntax
<?php
$somevar="Hello World";
echo <<<HTML
<b> Hey this is some text and I am adding this variable $somevar</b>
HTML; //<--- As you can see there is a leading space
You can always use <script type="text/php">...</script>
With very old version of PHP, HEREDOC <<<HTML to open a string are not always supported.
If you use an old PHP, a workaround is to do:
$myvar="some html code here link
next line of html code
".$myotherphpvar."
rest of lines"
Note that if you open string with ", you must escape all " for html code with \.
With recent PHP version you can use HEREDOC syntax. This is example of syntax:
$myvar=<<<HTML
some html code here link
next line of html code $myotherphpvar
rest of lines
HTML;
More examples on page:
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
This question already has answers here:
PHP - how to create a newline character?
(15 answers)
Closed 9 years ago.
I've tried every method to output a newline in PHP. Why doesn't the following work? :
<?php
$foo = 'bar';
echo "Hello \n $foo!";
?>
This should output a newline between hello and bar but it isn't.
I also tried \r\n instead of \n
If you using it as command line script this would work. I would use PHP_EOL for this since it chooses the right line break for the OS.
However if you are working with HTML (viewing the result in browser for example) you have to use the HTML way of linebreaks which is: <br />
To output a new line in HTML you need to use HTML's representation of a new line which is <br/>
php has a function for you that converts all natural new lines to HTML new lines > nl2br()
Then your code should look like
<?php
$foo = 'bar';
echo nl2br("Hello \n $foo!");
?>
Use <br> when inserting line break in html
To output a new line visually (in a browser), you need HTML:
echo "Hello\n<br />$foo!";
\n is a system line feed.
Try this:
<?php
$foo = 'bar';
echo "Hello <br> $foo!";,
?>
if you view the source you will see it actually DOES have a new line in it, But white space doesnt matter in HTML (what the browser displaying the output is expecting) but adding a <br /> to your statement will produce a new line because this is the way browsers read new lines and display them.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Have I misunderstood what heredoc should do?
I read that Here document preserves the line breaks
and other whitespace (including indentation) in the text. But when I run the following script,everything gets printed on the same line. Why is it so ?
<?php
$str = <<<HDC
This is a sample text
Some more sample text
Even more sample text
HDC;
echo $str;
The output actually does contain the line breaks. However, HTML (by default) ignores line breaks.
If you want the HTML to render the line breaks, wrap it in a pre:
<pre>
<?php
$str = <<<HDC
This is a sample text
Some more sample text
Even more sample text
HDC;
echo $str;
?>
</pre>
Or if you cannot use <pre> because <pre> always prints in fixed width, manually insert the <br> tags.
For some strange reason, inserting echo "\n"; and other scape sequence characters are not working for me, that's why I am just using <br /> instead.
The images of the results of examples in books and other documentations seems just alright. I'm currently using XAMPP and already used WAMPP with the same actual result. Why is that?
Edit:
It seems that I cannot understand the concept after comparing your answers with this:
PHP Linefeeds (\n) Not Working
Edit:
Sorry I didn't realized that the link above is referring to a php code writing to a file. I just wonder why I have these few php sample programs that uses \n even though it outputs in a webpage. Thanks everyone.
When you run a PHP script in a browser, it will be rendered as HTML by default. If the books you’re using show otherwise, then either the code or the illustration is inaccurate. You can use “view source” to view what was sent to the browser and you’ll see that your line feeds are present.
<?php
echo "Line 1\nLine 2";
?>
This will render in your browser as:
Line 1 Line 2
If you need to send plain text to your browser, you can use something like:
<?php
header('Content-type: text/plain');
echo "Line 1\nLine 2";
?>
This will output:
Line 1
Line 2
PHP Linefeeds (\n) Not Working is referring to sending output to a file rather than the browser.
You should be looking for nl2br(). This will add line breaks (<br>) to your output which will be rendered by the browser; newlines are not.
The echo "\n" is probably working, just not the way you expect it to.
That command will insert a new line character. From the sounds of it, you're using a browser to view your output. Note that if you wrote an HTML file that had a body contents that looked like:
<p>This
is
a
test </p>
The browser rendering would not include the new lines, and would instead just show "This is a test"
If you want to see the newlines, you could view source, and you'll see that the source code includes the new lines.
The rule of thumb is that if you need new lines in a browser, you need to use HTML (e.g. <br />), while if you want it in plain text, you can use the \n
<br /> is the HTML Tag for new line, whereas
"\n" is to output a new line (for real).
The browser doesn't output a new line each time the HTML file goes to the next line.
You can use the nl2br function to convert \n to <br>
As said before, HTML does not render \n as new line. It only recognizes the <br> html tag
If you are working with HTML (viewing the result in browser for example) you have to use the HTML way of linebreaks which is: <br>
/n only works if it is used as a simple text but here as we code in a html doc it takes it as a HTML text hence you can use </br> tag instead.
PHP outputs on the browser and browser only render output in HTML, any other output format will be ignored by the browser.
If you want browser to keep your standard output format as it is, you should enclose your output under HTML's <pre> tag. It preserves the formatting:
echo "<pre>";
echo "This is first line\nThis is new line";
echo "</pre>";
This will be rendered as
This is first line
This is new line
Alternatively, you can mention content type to be plain text in the header:
header('Content-type: text/plain');
echo "This is first line\nThis is new line";
This will tell the browser to render the output as plain text. And the browser will encolse the output automatically in <pre> tag.
solution is echo nl2br or=> <br>