Since both are acceptable by HTML as well as languages like ASP.NET and PHP when using attributes or strings, why is it that some people use single quotes and double quotes interchangeably?
因为两者都可以被HTML和像ASP这样的语言所接受。NET和PHP在使用属性或字符串时,为什么有些人可以互换使用单引号和双引号?
It is my understanding that it is syntactically correct to use double quotes where possible, single when you need to embed a double quote for inline logic.
我的理解是,在可能的情况下使用双引号是语法正确的,当您需要将双引号嵌入到内联逻辑中时,可以使用单引号。
Is there something I am missing?
有什么东西我遗漏了吗?
For examples:
例子:
HTML
HTML
<a href='http://google.com'>Google</a>
PHP
PHP
<? echo 'Hello World!'; ?>
ASP.NET
ASP.NET
<form id='myForm' runat='server'></form>
7 个解决方案
#1
1
In HTML, I don't think the "why" can be answered in anything but the obvious case: single quoted strings are more convenient when the string contains double quotes, and vice-versa.
在HTML中,我不认为“为什么”可以在任何情况下回答:当字符串包含双引号时,单引号字符串更方便,反之亦然。
In PHP, single quoted strings are more convenient when you don't want any special interpolation or escape characters.
在PHP中,当不需要任何特殊的插入或转义字符时,单引号字符串更方便。
My personal preference is always use double quotes in HTML, and to always use single quotes in PHP unless I need interpolation. Thus, I consider the single quoted strings to be "constants" of sorts in PHP, while the double quoted string implies something else is going on.
我个人的偏好是在HTML中使用双引号,在PHP中使用单引号,除非我需要插入。因此,我认为单引号字符串在PHP中是各种“常量”,而双引号字符串则意味着其他事情正在发生。
<opinion>But why do some people whimsically choose between the two? Probably because they are undisciplined and subsequently not very good programmers.</opinion>
<观点> 但是为什么有些人会异想天开地在两者之间做出选择?可能是因为他们没有纪律,而且随后也不是很好的程序员
#2
2
Technically, in PHP single quotes are faster since you don't need to parse the content within.
从技术上讲,在PHP中单引号更快,因为不需要解析其中的内容。
edit: So double quotes are automatically converted to single quotes, but if you have variable substitution going on within your double quoted string, that's when you take a performance hit:
编辑:双引号会自动转换为单引号,但是如果在双引号字符串中有变量替换,那么就会出现性能问题:
http://www.codeforest.net/php-myth-busters-using-single-quotes-on-string-is-faster-then-double-quotes
Either ways, to answer OP's question while the jury is out on this, play it safe (TM) and use single quotes :)
无论哪种方式,在陪审团还没有做出结论的时候,回答OP的问题,都要谨慎(TM),使用单引号:
#3
0
From W3C: http://www.w3.org/TR/html4/intro/sgmltut.html
W3C:http://www.w3.org/TR/html4/intro/sgmltut.html
All attribute values [should] be delimited using either double quotation
marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39).
Single quote marks can be included within the attribute value when the
value is delimited by double quote marks, and vice versa.
Strings in PHP follow the same principle - interchangeable single/double quotes.
PHP中的字符串遵循相同的原则——可互换单引号/双引号。
#4
0
I would say that for most people, single and double quotes are treating and used interchangeably without a real understanding of the difference.
我想说,对于大多数人来说,单引号和双引号是可以互换使用的,但并没有真正理解它们的区别。
Both are used to create/delineate strings.
两者都用于创建/描述字符串。
'Hello'
"Hello"
Both are strings and are treated the same when used in this circumstance.
两者都是字符串,在这种情况下使用时都是相同的。
The difference in in processing. Technically, single quotes strings are not processed when created and stored in to memory. They are taken as is and made into strings.
处理上的不同。从技术上讲,当创建并存储到内存中时,不会处理单引号字符串。它们被当作是真实的,并被制成字符串。
Double quoted strings are processed when created and stored into memory. That is why you can put a variable into a double quoted string and it's value will be put in, but in a single quoted string the literal variable is put in. For most things, there is not a real difference if you sing a single or double quote except when creating strings with variables, function calls, etc and for saving some milliseconds in processing.
当创建并存储到内存中时,将处理双引号字符串。这就是为什么你可以将一个变量放入一个双引号的字符串中,它的值将被放入,但是在一个单引号的字符串中,文字变量将被放入。对于大多数情况,如果您唱一个单引号或双引号,除了在创建带有变量、函数调用等的字符串并在处理过程中节省一些毫秒之外,没有什么真正的区别。
#5
0
Basically, the choice is yours. But for readability & maintainability, pick one form & stick with it.
基本上,选择权在你手中。但是为了可读性和可维护性,选择一种形式并坚持使用它。
#6
0
I find the use of single quotes advantageous when I'm embedding html into strings, mainly when dealing with templating. Here is an example:
当我将html嵌入字符串时,主要是在处理模板时,我发现使用单引号是有利的。这是一个例子:
public string EmailTemplate =
@"<div style='color:red'>HEY {0}! BUY MORE STUFF</div>"
// later in code
instanceOfStringBuilder.AppendFormat(EmailTemplate, firstNameVariable);
I don't have any hierarchy in my mind for whether single or double quotes are "better." It is purely a matter of being consistent and having something that programmatically works.
我对单引号和双引号是否“更好”没有任何概念。这纯粹是一个保持一致的问题,并且有一些程序上可以工作的东西。
#7
0
I agree with @stillstanding on the issue of interchangeability within HTML. However, in PHP I use double quotes in instances where I need a variable within a string parsed. Consider this:
我同意@stillstanding on the issue of interchangability within HTML。但是,在PHP中,我在需要解析字符串中的变量时使用双引号。考虑一下:
<?php
$id = 123;
echo "Your id is $id<br />";
echo 'Your id is $id';
?>
This will output
这将输出
Your id is 123
你的身份证是123
Your id is $id
你的身份证是$ id
#1
1
In HTML, I don't think the "why" can be answered in anything but the obvious case: single quoted strings are more convenient when the string contains double quotes, and vice-versa.
在HTML中,我不认为“为什么”可以在任何情况下回答:当字符串包含双引号时,单引号字符串更方便,反之亦然。
In PHP, single quoted strings are more convenient when you don't want any special interpolation or escape characters.
在PHP中,当不需要任何特殊的插入或转义字符时,单引号字符串更方便。
My personal preference is always use double quotes in HTML, and to always use single quotes in PHP unless I need interpolation. Thus, I consider the single quoted strings to be "constants" of sorts in PHP, while the double quoted string implies something else is going on.
我个人的偏好是在HTML中使用双引号,在PHP中使用单引号,除非我需要插入。因此,我认为单引号字符串在PHP中是各种“常量”,而双引号字符串则意味着其他事情正在发生。
<opinion>But why do some people whimsically choose between the two? Probably because they are undisciplined and subsequently not very good programmers.</opinion>
<观点> 但是为什么有些人会异想天开地在两者之间做出选择?可能是因为他们没有纪律,而且随后也不是很好的程序员
#2
2
Technically, in PHP single quotes are faster since you don't need to parse the content within.
从技术上讲,在PHP中单引号更快,因为不需要解析其中的内容。
edit: So double quotes are automatically converted to single quotes, but if you have variable substitution going on within your double quoted string, that's when you take a performance hit:
编辑:双引号会自动转换为单引号,但是如果在双引号字符串中有变量替换,那么就会出现性能问题:
http://www.codeforest.net/php-myth-busters-using-single-quotes-on-string-is-faster-then-double-quotes
Either ways, to answer OP's question while the jury is out on this, play it safe (TM) and use single quotes :)
无论哪种方式,在陪审团还没有做出结论的时候,回答OP的问题,都要谨慎(TM),使用单引号:
#3
0
From W3C: http://www.w3.org/TR/html4/intro/sgmltut.html
W3C:http://www.w3.org/TR/html4/intro/sgmltut.html
All attribute values [should] be delimited using either double quotation
marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39).
Single quote marks can be included within the attribute value when the
value is delimited by double quote marks, and vice versa.
Strings in PHP follow the same principle - interchangeable single/double quotes.
PHP中的字符串遵循相同的原则——可互换单引号/双引号。
#4
0
I would say that for most people, single and double quotes are treating and used interchangeably without a real understanding of the difference.
我想说,对于大多数人来说,单引号和双引号是可以互换使用的,但并没有真正理解它们的区别。
Both are used to create/delineate strings.
两者都用于创建/描述字符串。
'Hello'
"Hello"
Both are strings and are treated the same when used in this circumstance.
两者都是字符串,在这种情况下使用时都是相同的。
The difference in in processing. Technically, single quotes strings are not processed when created and stored in to memory. They are taken as is and made into strings.
处理上的不同。从技术上讲,当创建并存储到内存中时,不会处理单引号字符串。它们被当作是真实的,并被制成字符串。
Double quoted strings are processed when created and stored into memory. That is why you can put a variable into a double quoted string and it's value will be put in, but in a single quoted string the literal variable is put in. For most things, there is not a real difference if you sing a single or double quote except when creating strings with variables, function calls, etc and for saving some milliseconds in processing.
当创建并存储到内存中时,将处理双引号字符串。这就是为什么你可以将一个变量放入一个双引号的字符串中,它的值将被放入,但是在一个单引号的字符串中,文字变量将被放入。对于大多数情况,如果您唱一个单引号或双引号,除了在创建带有变量、函数调用等的字符串并在处理过程中节省一些毫秒之外,没有什么真正的区别。
#5
0
Basically, the choice is yours. But for readability & maintainability, pick one form & stick with it.
基本上,选择权在你手中。但是为了可读性和可维护性,选择一种形式并坚持使用它。
#6
0
I find the use of single quotes advantageous when I'm embedding html into strings, mainly when dealing with templating. Here is an example:
当我将html嵌入字符串时,主要是在处理模板时,我发现使用单引号是有利的。这是一个例子:
public string EmailTemplate =
@"<div style='color:red'>HEY {0}! BUY MORE STUFF</div>"
// later in code
instanceOfStringBuilder.AppendFormat(EmailTemplate, firstNameVariable);
I don't have any hierarchy in my mind for whether single or double quotes are "better." It is purely a matter of being consistent and having something that programmatically works.
我对单引号和双引号是否“更好”没有任何概念。这纯粹是一个保持一致的问题,并且有一些程序上可以工作的东西。
#7
0
I agree with @stillstanding on the issue of interchangeability within HTML. However, in PHP I use double quotes in instances where I need a variable within a string parsed. Consider this:
我同意@stillstanding on the issue of interchangability within HTML。但是,在PHP中,我在需要解析字符串中的变量时使用双引号。考虑一下:
<?php
$id = 123;
echo "Your id is $id<br />";
echo 'Your id is $id';
?>
This will output
这将输出
Your id is 123
你的身份证是123
Your id is $id
你的身份证是$ id