Print newline in PHP in single quotes
No, because single-quotes even inhibit hex code replacement.
echo 'Hello, world!' . "\xA";
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).
Making a new line with single quotes?
You have to use double quotes. Otherwise, PHP will literally output \n
.
http://php.net/manual/en/language.types.string.php
PHP - how to print newline?
Here:
$xml = $xml.'</product>';
PHP_EOL;
You have to concatenate that PHP_EOL, like this:
$xml = $xml . '</product>' . PHP_EOL;
But, if you want to show the new line on the screen, then use <br>
:
$xml = $xml . '</product>' . '<br>';
Php echo not printing newline even within double quotes or using PHP_EOL
if you want to use HTML tag inside php, try this:
echo "dkljaks aalksja klajklsa<br/>";
echo "abc jjka kajkajs<br/>";
and see here for br tag and if you don't want to use HTML tag. try this: nl2br() php function for line break.
\n or \n in php echo not print
PHP only interprets escaped characters (with the exception of the escaped backslash \\
and the escaped single quote \'
) when in double quotes ("
)
This works (results in a newline):
"\n"
This does not result in a newline:
'\n'
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
Http Options Request on Azure Websites Fails Due to Cors
Preg_Match() Unknown Modifier '[' Help
What Factors Make PHP Unicode-Incompatible
How to Place Two Forms on the Same Page
Setcookie() Does Not Set Cookie in Google Chrome
_Construct() VS Sameasclassname() for Constructor in PHP
How to Open a Link in Private Browsing Mode
PHP Upload, Extract and Progressbar
Get Next and Previous Day with PHP
How to Handle $_Post Data in MVC
Display Message Before Redirect to Other Page