PHP Echo Line Breaks

How to add a line break within echo in PHP?

\n is a line break. /n is not.


use of \n with

1. echo directly to page

Now if you are trying to echo string to the page:

echo  "kings \n garden";

output will be:

kings garden

you won't get garden in new line because PHP is a server-side language, and you are sending output as HTML, you need to create line breaks in HTML. HTML doesn't understand \n. You need to use the nl2br() function for that.

What it does is:

Returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).

echo  nl2br ("kings \n garden");

Output

kings
garden

Note Make sure you're echoing/printing \n in double quotes, else it will be rendered literally as \n. because php interpreter parse string in single quote with concept of as is

so "\n" not '\n'

2. write to text file

Now if you echo to text file you can use just \n and it will echo to a new line, like:

$myfile = fopen("test.txt", "w+")  ;

$txt = "kings \n garden";
fwrite($myfile, $txt);
fclose($myfile);

output will be:

kings
garden

How do I echo line breaks from a MySQL database?

PHP function
nl2br
converts newlines to "<br>" breaks.


So, $wallpost=nl2br($wallpost);
should accomplish the task.

How to echo an input of an textarea with line breaks?

When displaying text, use nl2br() to convert newlines to <br/> tags, i.e., instead of <?php echo $row['text']; ?>, use <?php echo nl2br($row['text']); ?>.

By default, browsers display newlines as spaces, therefore they have to be converted to <br/> tags.


For those who find this useful - please consider using white-space: pre-line, suggested by Emil Vikström. I'm not a web guy anymore and easily can't verify this, but Boaz says in comments that it is supported by all modern browsers. If so, that should be preferred to using nl2br().

m trying to echo a text into one line with no line breaks

Other than not using nl2br, and needing to replace with a space, not an empty string, your code should work fine (note you can optimise the preg_replace by using \R which matches any unicode newline sequence):

$mytext = "I'm trying to write my text\r
in one line";

echo preg_replace('/\R/',' ', $mytext);
echo PHP_EOL;
echo str_replace(array("\r\n", "\r","\n"), " ", $mytext);

Output:

I'm trying to write my text in one line
I'm trying to write my text in one line

Now if you want to make sure it remains on one line in the HTML, you need to replace spaces with non-breaking spaces e.g.

$mytext = str_replace(' ', ' ', $mytext);

Here's a demo using   in JS:





let str = "I'm trying to write my text in one line I'm trying to write my text in one line I'm trying to write my text in one line";

$('#d1').html(str);

str = str.replace(/\s/g, ' ');

$('#d2').html(str);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="d1" style="width: 200px;"></div>

<div id="d2" style="width: 200px;"></div>

PHP Echo Line Breaks


  • \n is a Linux/Unix line break.
  • \r is a classic Mac OS (non-OS X) line break. Mac OS X uses the above unix \n.
  • \r\n is a Windows line break.

I usually just use \n on our Linux systems and most Windows apps deal with it ok anyway.

How To echo a line break in the browser?

IF you are looking this output from a browser, you must use <br /> tag or, put <pre> before your echo.

For new line
example.

if (file_exists($fName)) {
echo "CreationTime: ".$CreationTime. "<br />".
"CurrentTime: ".$CurrentTime. "<br />" .
"after ".($fLifeTime)." Days from Creation: ".$fAge;
}

pre example

 if (file_exists($fName)) {
echo "<pre>";
echo "CreationTime: ".$CreationTime. PHP_EOL .
"CurrentTime: ".$CurrentTime. PHP_EOL .
"after ".($fLifeTime)." Days from Creation: ".$fAge;
}

php, wordpress add line break with in two echo statements


<?php if (is_page_template()) { 
echo get_page_template_slug() . '</br>';
echo get_page_template();
?>

try this

PHP - how to create a newline character?

Only double quoted strings interpret the escape sequences \r and \n as '0x0D' and '0x0A' respectively, so you want:

"\r\n"

Single quoted strings, on the other hand, only know the escape sequences \\ and \'.

So unless you concatenate the single quoted string with a line break generated elsewhere (e. g., using double quoted string "\r\n" or using chr function chr(0x0D).chr(0x0A)), the only other way to have a line break within a single quoted string is to literally type it with your editor:

$s = 'some text before the line break
some text after';

Make sure to check your editor for its line break settings if you require some specific character sequence (\r\n for example).

PHP Line Break not working

If you want to be sure your line break is cross platform compatible, use PHP_EOL I usually use "\n". If you want the linebreak to appear in HTML, use "<br>".

Edit in response to comments:
Here's how you pull as much PHP out of the HTML as possible.

<?php 

//Global varaible called after function
$g_test_string="this is the value in the variable outside the function at a global level!";
?>
<html>
<head>
<title>Testing Global and Local Variables</title>
</head>
<body>
<?php
//Calling statements

printstring();
echo "<br>\n";
echo 'Global variable value: ' . $g_test_string."<br>\n";

echo 'Local variable value: ' . $l_test_string."<br>\n";
?>
</body>
</html>

<?php
function printstring()
{
$l_test_string="this is the value of the variable inside the function at a local level!";
print($l_test_string);
}


Related Topics



Leave a reply



Submit