PHP基本连接问题?
<?php echo ($i % 6 == 5) ? 'style=\"margin-right:0px\"' : ''; ?>
我只是在视口上打印了 style="" 。
更新: 为什么当我们在字符串中时不需要转义双引号? 因为双引号永远不会被视为字符串以外的任何东西,如果它们位于单引号内?
提前致谢, MEM
<?php echo ($i % 6 == 5) ? 'style=\"margin-right:0px\"' : ''; ?>
I just get style="" printed on the view port.
Update:
Why is unnecessary to escape double quotes when we are inside a string?
Because double quotes will never be taken as anything else then a string, if they are inside single quotes?
Thanks in advance,
MEM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
手册,关于单引号字符串:
The manual, regarding single quoted strings:
无需在单引号内转义双引号。
您只需要转义单引号内的单引号或双引号内的双引号。如果要在单引号字符串中写入单引号,该单引号将终止该字符串。
PHP 将此视为字符串
a
,后跟无意义的b
以及永远不会终止的字符串';
的开头;这是无效的语法。这被正确解析为字符串
a'b
。此时你已经逃避了引文通常的含义。使用单引号内的双引号,就不会出现这样的歧义。单引号字符串中的双引号不会终止该字符串,它没有需要转义的特殊含义。如果包含反斜杠,则按字面意思使用反斜杠。
我想问题在于你如何看待输出。如果输出为
style=\"…\"
,则转义双引号可能会导致您查看输出的环境中出现无效语法。There's no need to escape double quotes within single quotes.
You only need to escape single quotes within single quotes or double quotes within double quotes. If you want to write a single quote within a single quoted string, that single quote would terminate the string.
PHP sees this as the string
a
, followed by a meaninglessb
and the start of the string';
which is never terminated; which is invalid syntax.This is correctly parsed as the string
a'b
. You have escaped the meaning the quote would usually have at this point.With double quotes within single quotes, there's no such ambiguity. A double quote within a single quoted string does not terminate the string, it has no such special meaning there that would need escaping. If you include a backslash, the backslash is used literally.
I suppose the problem is how you look at the output. If the output is
style=\"…\"
, the escaped double quotes might cause invalid syntax in the environment where you're looking at the output.您对该字符串使用了单引号
'
,因此无需在字符串内转义双引号"
。将其替换为'style="margin-right:0px "'
应该可以正常工作。为了更好地解释 PHP 如何处理字符串和引号,了解
'
和"
之间的区别会很有帮助。用'
封装的字符串始终按原样显示。字符串内的任何内容都不会被解析,包括任何转义字符(例如用于换行符的\n
或转义的引号,除了转义的单引号\'
)。相反,封装在"
中的字符串会被解析,因此如果有任何转义字符,它们将正确显示,如果字符串中有任何变量,它们也会被输入。例如,It使用单引号需要更少的处理能力,因为 PHP 不需要扫描字符串来转义任何内容,它只需要找到字符串的结尾。但是,如果您确实需要解析字符串内的内容,请确保这样做。使用双引号。
You used single-quotes
'
for that string, so escaping double quotes"
inside the string is unnecessary. Replace that with'style="margin-right:0px"'
and it should work just fine.To explain how PHP handles strings and quotes a bit better, it's helpful to know the difference between
'
and"
. Strings encapsulated with'
are always shown as-is. Nothing inside the string is parsed, including any escape characters (like\n
for a newline or escaped quotes, except for escaped single quotes\'
). Conversely, strings encapsulated in"
are parsed, so if you have any escape characters they will be displayed properly, and if you have any variables within the string, they will be entered as well. For example,It takes less processing power to use single quotes, since PHP won't need to scan the string to escape anything, it just needs to find the end of the string. However, if you do need to parse things inside the string, make sure to use double quotes.