PHP Linefeeds (\N) Not Working

PHP Linefeeds (\n) Not Working

Use double quotes. "test\n" will work just fine (Or, use 'test' . PHP_EOL).

If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:

http://php.net/manual/en/language.types.string.php

PHP new line character (\n) Not Working

This is the first thing you will learn if you are learning even from a PHP 5 For Dummies book. HTML doesn't respect new line or tab or multiple space characters. You have to use <br /> for new lines.

preview
* Sourced from PHP 5 For Dummies by Janet Valade.

Change your code to:

<?php
$firstName = 'David';
$lastName = "Powers";
$title = '"The Hitchhiker\'s Guide to the Galaxy"';
$author = 'Douglas Adams';
$answer = 42;
$newLines = "<br /><br />";

$fullName = "$firstName $lastName |";
$book = "$title by $author";

$message = "Name: $fullName $newLines";
$message .= "Book: $book <br /><br />";
$message .= "Answer: $answer";

echo $message;
echo "Line 1<br />Line 2";

If you are just opting for a text based layout, you can set the header to the browser to respect it as just a text file. For that, you need:

header("Content-type: text/plain");

This will render without any HTML.

New line (\n) in PHP is not working

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.

Line breaks not working in php string

\n only works in a double quoted string i.e. echo "\n"; or in your case

$style = sprintf("<link href='%s' rel='stylesheet' type='text/css' />\n", $style);

Or done more simply

$this->print_html .= "<link href='$style' rel='stylesheet' type='text/css' />\n";

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).

New lines (\r\n) are not working in email body

You need to use a <br> because your Content-Type is text/html.

It works without the Content-Type header because then your e-mail will be interpreted as plain text. If you really want to use \n you should use Content-Type: text/plain but then you'll lose any markup.

Also check out similar question here.

Line break in php wordpress not working

Replace the \n with <br />.

You are sending it as HTML, so use HTML linebreaks.

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


Related Topics



Leave a reply



Submit