Reputation: 11
When I am testing the following code
s$stringOne="This is what the \n escape characters are \n in it";
echo $stringOne;
$strong3="testing the escape characters $100 's \n $stringOne ";nippet it doesn't show on new line
Upvotes: 0
Views: 2646
Reputation: 19513
It most likely does work correctly. However, if you're viewing the result in a browser, it will be interpreted as HTML, and HTML treats newlines in text just like spaces, so you won't see those newlines outside of the page source code.
If you want a line break in HTML, you'll need to use a <br>
element. They can be used like this:
$stringOne = "This is what the <br>\n escape characters are <br>\n in it";
echo $stringOne;
You can also make PHP add <br>
elements for you with the nl2br
function, e.g.:
$stringOne = "This is what the \n escape characters are \n in it";
echo nl2br($stringOne);
Another option in HTML is to put your text inside a <pre>
element, which makes the browser show newlines and other spaces as they appear in the source code. For example:
$stringOne = "<pre>This is what the \n escape characters are \n in it</pre>";
echo $stringOne;
If your output isn't intended as HTML and you actually meant it as plain text, you need to tell the browser that with header('Content-Type: plain/text,charset=UTF-8');
at the top of your PHP script:
<?php
header('Content-Type: plain/text,charset=UTF-8');
$stringOne = "This is what the \n escape characters are \n in it";
echo $stringOne;
Upvotes: 4