我应该对单引号使用 php 引号转义还是在数组中使用双引号?

发布于 2024-08-23 04:03:24 字数 394 浏览 4 评论 0原文

我意识到 php 数组中的单引号可能会出现问题:

<?php
$lang = array(
    'tagline' => 'Let's start your project',
    "h1" => "About Me"
);
?>

所以我将其更改为双引号:

<?php
$lang = array(
    "tagline" => "Let's start your project",
    "h1" => "About Me"
);
?>

我应该使用“php quote escapes”,而不是我刚才所做的吗? (顺便问一下“php quote escapes”怎么写?)

I realized that I can have problems with single quotes in php arrays:

<?php
$lang = array(
    'tagline' => 'Let's start your project',
    "h1" => "About Me"
);
?>

So I changed it to double quotes:

<?php
$lang = array(
    "tagline" => "Let's start your project",
    "h1" => "About Me"
);
?>

Should I use "php quote escapes", instead of what I just did?
(by the way how to write "php quote escapes"?)

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

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

发布评论

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

评论(2

池木 2024-08-30 04:03:24

首先,有人会说单引号字符串比双引号字符串更快;您不应该关心这种微优化:它不会对您的应用程序产生任何影响。

单引号字符串和双引号字符串之间的区别是:

  • 对于双引号字符串,双引号字符串存在变量插值
  • ,可以使用一些特殊字符,例如 \n\ t, ...
  • 对于单引号字符串,您可以使用简单引号进行转义。

作为参考,在 PHP 手册中:

我认为,一般而言,这主要是个人喜好的问题...

就个人而言,在您所描述的情况下,我会使用双引号字符串,就像您所做的那样:它使代码更容易既可以写也可以读,因为您不必逃避这句话。

First of all, some people will say that simple-quoted strings are faster that double-quoted strings ; you should not care about that kind of micro-optimization : it will not make any difference for your application.

The difference between simple-quoted and double-quoted strings is :

  • with double-quoted strings, there is variable interpolations
  • with double-quoted strings, you can use some special characters like \n, \t, ...
  • with single-quoted strings, you have simple-quotes to escape.

For reference, in the PHP manual :

I would that that, in the general matter, it's mostly a matter of personnal preferences...

Personnaly, in a situation such as the one you described, I would use a double-quoted string, like you did : it make the code easier to both write and read, as you don't have to escape that quote.

时光沙漏 2024-08-30 04:03:24

假设您不需要双引号特殊功能,请执行最具可读性的操作。对我来说,这是使用字符串中出现最少的字符串的引号。

Do whatever is most readable, assuming you don't need the double-quote special features. For me, that's using whichever quotes for the string that appear the least in the string.

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