search

Home  >  Q&A  >  body text

What is the difference between single and double quoted strings in PHP?

<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>
P粉773659687P粉773659687463 days ago810

reply all(2)I'll reply

  • P粉458725040

    P粉4587250402023-08-24 13:14:41

    Things are evaluated in double quotes, but not in single quotes:

    $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.

    reply
    0
  • P粉323374878

    P粉3233748782023-08-24 10:55:52

    You can specify

    PHP stringsnot just two ways, but four ways.

    1. A single quoted string will display the content almost exactly "as is". Variables and most escape sequences will not be interpreted. The exceptions are that to display a literal single quote you can escape it with a backslash \', and to display a backslash you can use another backslash \\ (so yes, even single quoted strings will be parsed ).
    2. Double-quoted strings will display a large number of escape sequences (including some regular expressions), and variables in the string will be evaluated. The important point here is that you can use curly braces to isolate the name of the variable that is being evaluated. For example, suppose you have the variable $type and you want to echo "The $types are" . This will look for the variable $types. To resolve this issue, use echo "The {$type} are". Take a look at String Parsing to learn how to use array variables etc.
    3. Heredoc String syntax works like double quoted strings. It starts with <<<<<<. 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.
    4. Nowdoc (Since PHP 5.3.0) String syntax works essentially like single quoted strings. The difference is that you don't even need to escape the single quotes or backslashes. nowdoc is identified using the same <<<<<< sequence as the document here, but the following identifier is enclosed in single quotes, such as <<<'EOT'<<<'EOT'. No parsing is done in nowdoc.

    Comments: 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 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

    for($i=0;$i<100000;$i++) {
        'string';
    }

    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.

    reply
    0
  • Cancelreply