我应该在字符串中使用大括号还是连接变量?

发布于 2024-10-11 22:04:08 字数 512 浏览 8 评论 0原文

在字符串中连接变量或使用花括号代替有优点还是缺点?

连接:

$greeting = "Welcome, " . $name . "!";

花括号:

$greeting = "Welcome, {$name}!";

就个人而言,我总是连接字符串,因为我使用 UEStudio,它在连接时用不同的颜色突出显示 PHP 变量。然而,当变量没有被分解时,它就不会。它只是让我的眼睛更容易在长字符串等中找到 PHP 变量。

人们对 SQL 感到困惑。这不是这个问题的内容。 不是 。我更新了示例以避免混淆。

Is there an advantage or disadvantage to concatenating variables within strings or using curly braces instead?

Concatenated:

$greeting = "Welcome, " . $name . "!";

Curly braces:

$greeting = "Welcome, {$name}!";

Personally, I've always concatenated my strings, because I use UEStudio, and it highlights PHP variables with a different color when concatenated. However, when the variable is not broken out, it does not. It just makes it easier for my eyes to find PHP variables in long strings, etc.

People are confusing this about being about SQL. This is not what this question is about. I've updated my examples to avoid confusion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

春庭雪 2024-10-18 22:04:08

如果您查看输出,以下所有内容都会执行相同的操作。

  1. $greeting = "欢迎," . $名称。 "!";
  2. $greeting = '欢迎,' . $名称。 '!';
  3. $greeting = "欢迎,$name!";
  4. $greeting = "欢迎,{$name}!";

你不应该使用选项 1,改用选项 2。选项 3 和 4 是相同的。对于简单变量,大括号是可选的。但如果使用数组元素,则必须使用大括号;例如:$greeting =“欢迎,{$user['name']}!”;。因此,作为标准,如果使用变量插值,则使用大括号而不是串联。

但如果使用制表符(\t)、换行符(\n)等字符,则必须在双引号内。

一般来说,变量插值很慢,但如果要连接的变量太多,连接也可能会更慢。因此,根据其他角色之间的变量数量来决定。

All of the following does the same if you look at the output.

  1. $greeting = "Welcome, " . $name . "!";
  2. $greeting = 'Welcome, ' . $name . '!';
  3. $greeting = "Welcome, $name!";
  4. $greeting = "Welcome, {$name}!";

You should not be using option 1, use option 2 instead. Both option 3 and 4 are the same. For a simple variable, braces are optional. But if you are using array elements, you must use braces; e.g.: $greeting = "Welcome, {$user['name']}!";. Therefore as a standard, braces are used if variable interpolation is used, instead of concatenation.

But if characters such as tab (\t), new-line (\n) are used, they must be within double quotations.

Generally variable interpolation is slow, but concatenation may also be slower if you have too many variables to concatenate. Therefore decide depending on how many variables among other characters.

回忆凄美了谁 2024-10-18 22:04:08

虽然不处理注入攻击(包括 SQLi),但至少应该注意(尤其是对于 PHP 开发人员),使用上述任何技术而不首先编码和验证所有输入将导致您基于注入的攻击。

重要的是要记住编码开始时的安全性,而不是在需要重做所有代码以符合安全要求时记住安全性。或者,当你最终得到这个 dang " vs. ' 战争并意识到这并不重要,因为使用任何一种技术都容易受到 XSS 攻击,而没有正确编码和验证所有输入。

  1. 编码使用
  2. >urlencode() 或 htmlenities()对非字符串字典查找和/或常规 要验证的字符串表达式

Although not dealing with injection attacks (including SQLi), it should at least be noted -- especially for PHP devs -- that using any of the above techniques without first encoding and validating all inputs will lead you to an injection-based attack.

It is important to remember security at the beginning of coding -- not the end when all of the code needs to be redone to comply with security requirements. Or, when you finally get this dang " vs. ' war down and realize that it doesn't matter because you are susceptible to XSS using either technique without properly encoding and validating all inputs.

  1. Encode using urlencode() or htmlenities() to normalize the input(s).
  2. Use data-typing for non-strings OR dictionary-lookup and/or regular expressions for strings to validate.
  3. Profit?
丶情人眼里出诗心の 2024-10-18 22:04:08

对于预编译的 PHP(字节码缓存)来说,这没有什么区别。

PHP 5.5 (Zend Optimizer+) 附带此功能。

With pre-comiled PHP (Bytecode Cache) it makes no difference.

This feature come with PHP 5.5 (Zend Optimizer+).

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