What is the difference between single and double quoted strings in PHP?
P粉773659687
2023-08-23 20:46:33
<p>I'm a little confused as to why I see some code in PHP where strings are placed in single quotes and sometimes in double quotes. </p>
<p>I only know that in .NET, or C, if it is in single quotes, that means it is a character, not a string. </p>
Things are evaluated in double quotes, but not in single quotes:
You can specify
PHP stringsnot just two ways, but four ways.
\'
, and to display a backslash you can use another backslash\\ code> (so yes, even single quoted strings will be parsed ).
$type
and you want toecho "The $types are"
. This will look for the variable$types
. To resolve this issue, useecho "The {$type} are"
. Take a look at String Parsing to learn how to use array variables etc.. After the operator, provide an identifier and then a newline character. Next is the string itself, and then the same identifier is used again to end the reference. You don't need to escape quotes in this syntax.
sequence as the document here, but the following identifier is enclosed in single quotes, such as
. No parsing is done in nowdoc.
Comments: Single quotes within single quotes and double quotes within double quotes must be escaped:
speed:
no difference.
Please read Trusted Article A PHP core developer talks about this issue. When it comes to tests, we should never take them for granted. It is important to understand that writing trustworthy tests and especially interpreting their results requires a great deal of knowledge and experience. This means that most tests are fake. For example, in code like this
Quoted strings are parsed only once along with the entire script and then converted to opcodes. Then it is executed a million times. So it measures anything except parsing. This is just the tip of the iceberg. With such nanobenchmarks, it's nearly impossible to create a reliable test that won't be corrupted by some disruptive side effects.