Search code examples
phpquotes

When should you use single or double quotes in PHP?


Forgive the bluntness of the question, but I've noticed really you can use either single or double quotes to begin with, along as you follow the suite throughout.

Someone once told me to use double quotes when echoing, outputting or returning, and single quotes for everything else.

But I'd like to know, is there a solid reason when to only use single quotes, or vice versa and not the other?


Solution

  • The only difference is that double quoted strings interpret embedded variables and a number of escape sequences, while single quoted strings do not. E.g.:

    'This is $a \n string'
    

    This is literally the string "This is $a \n string".

    "This is $a \n string"
    

    This is a string containing the value of variable $a and a line break.

    Use both as appropriate. If neither escape sequences nor variables are of interest to you, I'd default to single quoted strings; with one exception: if you need single quotes in the string, then double quotes are "cleaner". Compare:

    'I don\'t care.'
    "I don't care."