How to Add a Line Break Within Echo in PHP

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

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, 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

Elegant solution for line-breaks (PHP)

I ended up writing a function that has worked for me well so far:

// pretty print data
function out($data, $label = NULL) {

$CLI = (php_sapi_name() === 'cli') ? 'cli' : '';

$gettype = gettype($data);

if (isset($label)) {
if ($CLI) { $label = $label . ': '; }
else { $label = '<b>'.$label.'</b>: '; }
}

if ($gettype == 'string' || $gettype == 'integer' || $gettype == 'double' || $gettype == 'boolean') {
if ($CLI) { echo $label . $data . "\n"; }
else { echo $label . $data . "<br/>"; }
}
else {
if ($CLI) { echo $label . print_r($data,1) . "\n"; }
else { echo $label . "<pre>".print_r($data,1)."</pre>"; }
}
}

// Usage

out('Hello world!');

$var = 'Hello Stackoverflow!';
out($var, 'Label');

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;
}

how can I implement a line break in a variable in php?

You can use nl2br function of php:

$a = "apple \r\n orange \r\n banana";
echo nl2br($a);

Line Break within echo in a while loop

For a line break in HTML try using <br />, your code can look like something along this line:

echo "<ul>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

{
echo "<li><a href=\"http://mysite.com/{$row['row1']}/{$row['row2']} \">{$row['row3']}</a></li> <br />";
}
echo "</ul>";

EDIT

Also note, as mentioned before in the comments, <br /> isn't needed, if I take it out of my code my output stays the same.

I made a test php file similar and used my suggestion above and works fine, here is my code

<?php
echo "<ul>";
$i = 0;
do {
$i++;
echo "<li><a href=\"http://mysite.com/{$i}/{$i} \">{$i}</a></li> <br />";
} while($i < 10);
echo "</ul>";
?>

Displayed below:

See

Line break in PHP

There are two things you can do:

1: Use <br> as line break. If you use that, you should also consider using <hr> to insert the horizontal line.

some text<br>
<hr>
some other text<br>

2: Use <pre> and </pre> tags around your text to output preformatted text. You can then use \n and \t to format your text.

<pre>
some text\n
-------\n
some other text\n
</pre>

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.



Related Topics



Leave a reply



Submit