phpstringquotesdouble-quotessingle-quotes

Single quoted strings versus double quoted strings


I am really very confused about what is used to represent a string in PHP.

If we use double inverted commas, it too represents string and same if use single inverted commas so "avinash" or 'avinash'.

Which is a string?

Can you tell me about a good book to read PHP5 from?


Solution

  • Double quotes (") and single quotes (') both represent strings. However, PHP doesn't treat them the same way.

    In double quoted (") strings, escape sequences and variable names are expanded. In single quotes (') strings, they are not. The string is not expanded.

    So, given the following:

    $name = "Foo";
    

    The following code...

    $doubleQuotedString = "Hello $name.\nIt is nice to see you.";
    echo $doubleQuotedString;
    

    ... will output this:

    Hello Foo.
    It is nice to see you.

    However, the following...

    $singleQuotedString = 'Hello $name.\nIt is nice to see you.';
    echo $singleQuotedString;
    

    ... will output this:

    Hello $name.\nIt is nice to see you.

    For more information, you can read the following documentation page on strings.

    PHP Documentation: Strings