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


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

\n is not meant to be seen as a new line by the end user, you must use the html <br/> element for that. /n only affects how the html that is generated by php appears in the source code of the web page. if you go to your web page and click on 'view source' you will see php-generated html as one long line. Not pretty. That's what \n is for ; to break that php-generated html into shorter lines. The purpose of \n is to make a prettier 'view source' page.

Tags:

Php

Newline