PHP基本连接问题?

发布于 2024-10-04 18:00:10 字数 231 浏览 0 评论 0原文

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋竹姑娘 2024-10-11 18:00:11

为什么当我们在字符串中时不需要转义双引号?因为双引号永远不会被视为字符串以外的任何东西,如果它们位于单引号内?

手册,关于单引号字符串:

要指定文字单引号,请使用反斜杠 (\) 将其转义。要指定文字反斜杠,请将其加倍 (\\)。 反斜杠的所有其他实例将被视为文字反斜杠:这意味着您可能习惯的其他转义序列(例如 \r 或 \n)将按指定的字面形式输出,而不是具有任何特殊含义。

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.

缺⑴份安定 2024-10-11 18:00:11

无需在单引号内转义双引号。

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

您只需要转义单引号内的单引号或双引号内的双引号。如果要在单引号字符串中写入单引号,该单引号将终止该字符串。

$foo = 'a'b';

PHP 将此视为字符串 a,后跟无意义的 b 以及永远不会终止的字符串 '; 的开头;这是无效的语法。

$foo = 'a\'b';

这被正确解析为字符串 a'b。此时你已经逃避了引文通常的含义。

使用单引号内的双引号,就不会出现这样的歧义。单引号字符串中的双引号不会终止该字符串,它没有需要转义的特殊含义。如果包含反斜杠,则按字面意思使用反斜杠。

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

我想问题在于你如何看待输出。如果输出为 style=\"…\",则转义双引号可能会导致您查看输出的环境中出现无效语法。

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 '; which is never terminated; which is invalid syntax.

$foo = 'a\'b';

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.

$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.

可遇━不可求 2024-10-11 18:00:11

您对该字符串使用了单引号 ',因此无需在字符串内转义双引号 "。将其替换为 'style="margin-right:0px "' 应该可以正常工作。

为了更好地解释 PHP 如何处理字符串和引号,了解 '" 之间的区别会很有帮助。用 ' 封装的字符串始终按原样显示。字符串内的任何内容都不会被解析,包括任何转义字符(例如用于换行符的 \n 或转义的引号,除了转义的单引号 \')。相反,封装在 " 中的字符串会被解析,因此如果有任何转义字符,它们将正确显示,如果字符串中有任何变量,它们也会被输入。例如,

// 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使用单引号需要更少的处理能力,因为 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,

// 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. However, if you do need to parse things inside the string, make sure to use double quotes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文