In PHP, what is the difference between single and double quoted strings?
P粉604507867
P粉604507867 2023-08-18 11:45:01
0
2
669
<p>I'm a little confused why in PHP some strings in the code use single quotes and some use double quotes. </p> <p>I only know that in .NET or C, if you use single quotes, that means it's a character, not a string. </p>
P粉604507867
P粉604507867

reply all(2)
P粉311089279

The content in double quotes will be parsed, but the content in single quotes will not be parsed:

$s = "dollars";
echo 'This costs a lot of $s.'; // This costs a lot of $s.
echo "This costs a lot of $s."; // This costs a lot of dollars.
P粉741678385

PHP string can be specified not only in two ways, but also in four ways.

  1. Single quoted strings are displayed almost exactly as is. Variables and most escape sequences are not interpreted. The exception is that to display a single quote, escape it with a backslash \', and to display a backslash, escape it with another backslash \\ Escape (so, yes, even single-quoted strings will be parsed).
  2. Double-quoted stringA series of escape sequences (including some regular expressions) will be displayed, and the variables in the string will be evaluated. An important point is that you can use curly braces to isolate the name of the variable for which the value is required. For example, suppose you have a variable $type and you want to echo "The $types are". This will look for the variable $types. To solve this problem, use echo "The {$type}s are". Check out String Parsing to learn how to use array variables etc.
  3. HeredocString syntax is similar to double-quoted strings. It starts with <<<. After this operator, provide an identifier, followed by a newline character. Then the string itself, and the same identifier is used again to close the reference. In this syntax, you don't need to escape quotes.
  4. Nowdoc (Since PHP 5.3.0) String syntax is basically the same as single quoted strings. The difference is that you don't even need to escape the single quotes or backslashes. Nowdoc uses the same <<< sequence as heredoc, but the following identifier is enclosed in single quotes, such as <<<'EOT'. No parsing is done in Nowdoc.

Notice: Single quotes within single quotes and double quotes within double quotes must be escaped:

$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";

speed:
no difference. Please read a
Trusted Article from a core PHP developer. When it comes to testing, we should never take it for granted. It is important to understand that writing trustworthy tests and interpreting their results requires a lot of knowledge and experience. This means that most tests are fake. For example, in code like this

for($i=0;$i<100000;$i++) {
    'string';
}
The quoted string is parsed

only once , along with the entire script, and then converted to opcodes. Then do it a million times. Therefore, what it measures is not parsing. This is just the tip of the iceberg. With nanobenchmarks like this, it's nearly impossible to create a trustworthy test that won't be marred by some interfering side effects.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template