简体   繁体   English

PHP基本串联问题?

[英]PHP basic concatenation issue?

<?php echo ($i % 6 == 5) ? 'style=\"margin-right:0px\"' : ''; ?>

I just get style="" printed on the view port. 我只是在视口上打印了style =“”。

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 预先感谢,MEM

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? 因为双引号永远不会被当作字符串,如果它们在单引号之内,它将永远不会被当作字符串?

The manual , regarding single quoted strings: 关于单引号字符串的手册

To specify a literal single quote, escape it with a backslash (\\). 要指定文字单引号,请使用反斜杠(\\)对其进行转义。 To specify a literal backslash, double it (\\\\). 要指定文字反斜杠,请将其加倍(\\\\)。 All other instances of backslash will be treated as a literal backslash : this means that the other escape sequences you might be used to, such as \\r or \\n, will be output literally as specified rather than having any special meaning. 所有其他反斜杠实例都将被视为文字反斜杠 :这意味着您可能习惯的其他转义序列(例如\\ r或\\ n)将按照指定的字面意思输出,而不具有任何特殊含义。

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. 您在该字符串中使用了单引号' ,因此在字符串中使用双引号"是没有必要的。将其替换为'style="margin-right:0px"' ,它应该可以正常工作。

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. 为了解释PHP如何处理字符串和更好地使用引号,了解'"之间的区别是有帮助的。用'封装的字符串总是按原样显示。字符串内部不进行任何解析,包括任何转义字符(例如\\n换行符或转义的引号,但转义的单引号\\'除外。相反,封装在"中的字符串将被解析,因此如果您有任何转义字符,它们将被正确显示,并且如果您在字符串中包含任何变量,它们将被也进入了。 For example, 例如,

// Set name variable to my name
$name = "nhinkle";

// Echo hello name with single quotes
echo 'hello {$name}';
// The result will be "hello {$name}"

// Echo hello name with double quotes
echo "hello {$name}";
// The result will be "hello nhinkle"

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. 使用单引号需要较少的处理能力,因为PHP无需扫描字符串即可转义任何内容,因此只需查找字符串的结尾即可。 However, if you do need to parse things inside the string, make sure to use double quotes. 但是,如果确实需要解析字符串中的内容,请确保使用双引号。

There's no need to escape double quotes within single quotes. 无需在单引号内转义双引号。

<?php echo ($i % 6 == 5) ? 'style="margin-right:0px"' : ''; ?>

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. 如果要在单引号引起的字符串中写入单引号,则该单引号将终止字符串。

$foo = 'a'b';

PHP sees this as the string a , followed by a meaningless b and the start of the string '; PHP将其视为字符串a ,后跟无意义的b以及字符串';的开头'; which is never terminated; 永不终止; which is invalid syntax. 这是无效的语法。

$foo = 'a\'b';

This is correctly parsed as the string a'b . 正确地将其解析为字符串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. 如果包含反斜杠,则会按原样使用反斜杠。

$foo = 'a"b';  // a"b
$foo = 'a\"b'; // a\"b

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=\\"…\\" ,则在您查看输出的环境中,转义的双引号可能会导致语法无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM