RSS Feed

Know when to use double quotes – A second look

Based on the response I got from JaTaMan I thought it would be a good idea to add an additional post about single and double quote usage in PHP.

JaTaMan pointed out that a colleague of his used the new line special character in single quotes not knowing that single quotes are literal, what you see is what you get. Because of that I have listed some more detailed examples of usage between single and double quotes – basic but strict standards.

Strings that contain variables can be in double quotes or concatenated between single quotes:


$name = 'Steve Clarke';
$string = "My name is $name";
// My name is Steve Clarke


$name = 'Steve Clarke';
$string = 'My name is '.$name;
// My name is Steve Clarke

Using special characters such as newlines and tabs should be placed within double quotes as PHP parses double qoutes:


$string = "This is one line \nAnd this is a new line";
// This is one line
// And this is a new line


$string = 'This is one line \nAnd this is a new line';
// This is one line \nAnd this is a new line

Another important factor is escaping. If you need to use a single quote within a single quote string your will need to escape the single quote, like so:


$string = 'My name isn't James Bond';


$string = 'My name isn\'t James Bond';

You could use double quotes so that you do not need to escape the single quote however based on the main point of the two posts PHP will evaluate double quoted strings and add extra processing time, don’t waste time.

The same rule applies for double quotes:


$string = "My name isn't "James Bond"";


$string = "My name isn't \"James Bond\"";

A good rule of thumb to follow is simply use single quotes wherever you are not using special characters or variables. If you want to make this even easier on yourself only use double quotes when using special characters and concatenate variables to single quote strings.

Posted in PHP on the 15th April 2010

4 people have spoken their minds!

  1. I often only use single quotes because they parse faster. When you use duoble quotes PHP has to look if you aren’t using any variables inside the string. If I however need to use a variable i just add it.

    ‘this is a sentence’.$myVar

  2. Ivan Škugor says:


    using single quotes isn’t always faster. You can read more about that here:

    • srcnix says:

      Nice post there Ivan.

      I would like to point out that the magnitude of those results would increase heavily upon higher load. Also worth mentioning is peoples lack of knowledge on the difference between single and double quotes and this is the point of this article.

      However, good points have been made by the both of you, thank you.


Your email address will not be published. Required fields are marked *