简体   繁体   English

单引号而不是双引号?

[英]Single Quotes Instead of Double Quotes?

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>

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 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)并使用单引号:)

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. 我的个人偏好总是在HTML中使用双引号,并且总是在PHP中使用单引号,除非我需要插值。 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. 因此,我认为单引号字符串在PHP中是“常量”,而双引号字符串意味着其他东西正在发生。

<opinion>But why do some people whimsically choose between the two? <意见>但为什么有些人会异想天开地在两者之间做出选择呢? Probably because they are undisciplined and subsequently not very good programmers.</opinion> 可能是因为他们没有纪律,后来也不是很好的程序员。</ opinion>

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中的字符串遵循相同的原则 - 可互换的单/双引号。

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. 这就是为什么你可以把一个变量放到一个双引号字符串中它的值将被放入,但是在一个带引号的字符串中放入了文字变量。对于大多数情况,如果你唱一个或者一个或者没有真正的区别。双引号,除了用变量,函数调用等创建字符串以及在处理中保存几毫秒时。

Basically, the choice is yours. 基本上,选择是你的。 But for readability & maintainability, pick one form & stick with it. 但为了便于阅读和维护,请选择一种形式并坚持下去。

I find the use of single quotes advantageous when I'm embedding html into strings, mainly when dealing with templating. 当我将html嵌入到字符串中时,我发现使用单引号是有利的,主要是在处理模板时。 Here is an example: 这是一个例子:

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. 这纯粹是一致的问题,并且具有以编程方式工作的东西。

I agree with @stillstanding on the issue of interchangeability within HTML. 我同意@stillstanding关于HTML中的可互换性问题。 However, in PHP I use double quotes in instances where I need a variable within a string parsed. 但是,在PHP中,我在需要解析的字符串中的变量的实例中使用双引号。 Consider this: 考虑一下:

<?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

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

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