用单引号在 PHP 中打印换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2531969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print newline in PHP in single quotes
提问by Matt
I try to use single quotes as much as possible and I've noticed that I can't use \n in single quotes. I know I can just enter a newline literally by pressing return, but that screws up the indentation of my code.
我尽量使用单引号,但我注意到我不能在单引号中使用 \n。我知道我可以直接按回车键输入换行符,但这会破坏我的代码缩进。
Is there some ASCII character or something that I can type that will produce newline when I'm using single quotes?
当我使用单引号时,是否有一些 ASCII 字符或我可以输入的东西会产生换行符?
回答by Ignacio Vazquez-Abrams
No, because single-quotes even inhibit hex code replacement.
不,因为单引号甚至禁止十六进制代码替换。
echo 'Hello, world!' . "\xA";
回答by Cups
echo 'hollow world' . PHP_EOL;
Use the constant PHP_EOL then it is OS independent too.
使用常量 PHP_EOL 那么它也是独立于操作系统的。
回答by Anthony Forloney
If you are echoing to a browser, you can use <br/>with your statement:
如果您echo使用浏览器,则可以使用<br/>您的语句:
echo 'Will print a newline<br/>';
echo 'But this wont!';
回答by leepowers
FYI it is possible to get newlines into strings without double quotes:
仅供参考,可以在没有双引号的情况下将换行符放入字符串中:
printf('Please%1$sgive%1$sme%1$snewlines%1$s', PHP_EOL);
Which may be useful If your irrational fear of double quotes knows no bounds. Though I fear this cure may be worse than the disease.
如果您对双引号的非理性恐惧是无止境的,这可能很有用。虽然我担心这种治疗可能比疾病更糟糕。
回答by linuxdev
There IS a difference on using single VS double quotes in PHP
在 PHP 中使用单引号和双引号是有区别的
e.g:
1. echo '$var\n';2. echo "$var\n";
例如: 1. echo '$var\n';2.echo "$var\n";
- in 1, PHP will print literally:
$var\n - in 2, PHP will have to search the location in memory for
$var, and return the value in that location, also, it will have to parse the \n as a new line character and print that result
- 1、PHP 会逐字打印:
$var\n - 在 2 中,PHP 必须在内存中搜索位置
$var,并返回该位置的值,此外,它还必须将 \n 解析为换行符并打印该结果
We're in the range of millionths of a second, but there IS a difference in performance. I would recommend you to use single quotes whenever possible, even knowing you won't be able to perceive this performance increase. But I'm a paranoid developer when it comes to performance.
我们在百万分之一秒的范围内,但性能存在差异。我建议您尽可能使用单引号,即使知道您将无法感知这种性能提升。但在性能方面,我是一个偏执的开发人员。
回答by Alan Storm
The only escape sequence you can use in single quotes is for the single quote itself.
您可以在单引号中使用的唯一转义序列是用于单引号本身。
$foo = 'That\'s great';
The only way you could insert a new line into a string created with single quotes is to insert a literal newline
在用单引号创建的字符串中插入新行的唯一方法是插入文字换行符
$bar = 'That\'s
cheating';
回答by NewSites
I wonder why no one added the alternative of using the function chr():
我想知道为什么没有人添加使用该函数的替代方法chr():
echo 'Hello World!' . chr(10);
or, more efficient if you're going to repeat it a million times:
或者,如果你要重复一百万次,效率会更高:
define('C_NewLine', chr(10));
...
echo 'Hello World!' . C_NewLine;
This avoids the silly-looking notation of concatenating a single- and double-quoted string.
这避免了连接单引号和双引号字符串的看起来很傻的符号。
回答by Duniyadnd
You may want to consider using <<<
您可能需要考虑使用 <<<
e.g.
例如
<<<VARIABLE
this is some
random text
that I'm typing
here and I will end it with the
same word I started it with
VARIABLE
More info at: http://php.net/manual/en/language.types.string.php
更多信息请访问:http: //php.net/manual/en/language.types.string.php
Btw - Some Coding environments don't know how to handle the above syntax.
顺便说一句 - 一些编码环境不知道如何处理上述语法。
回答by Your Common Sense
No, according to documentation, PHP recognize no special symbol in single quotes. Andthere is no single reason to use single quotes as much as possible
不,根据文档,PHP 不识别单引号中的特殊符号。并且没有单一的理由尽可能多地使用单引号
回答by Braham Youssef
in case you have a variable :
如果你有一个变量:
$your_var = 'declare your var';
echo 'i want to show my var here'.$your_var.'<br>';

