Difference between single quote and double quote in php

Single quotes and double quotes are handled a bit different in PHP. Read on to see the differences in how they are evaluated.

Single Quotes

Single quotes tend to parse things in a much more literal sense. For example, take the following:

$test = "BOOOM";
echo '$test'

The variable name, when put within the single quotes is not parsed to its actual value. Here’s what’s actually printed from running this as a simple php program:


There are only 2 scenarios that I can think of where information actually gets parsed within a single quoted string. First, to display a single quoted character, it must be preceded by a backslash. Otherwise it will be interpreted as the end of your string. Second, to display a backslash, it must be preceded by another backslack. Example:

echo 'single quote: \'';
echo 'backslash: \\';

Double Quotes

Double quotes tend to parse things out for you. When run between double quotes:

$test = "BOOOM";
echo "$test"

It outputs the actual variable value:


Now, occasionally you will run into a scenario where the variable name you are trying to output is caused to not be parsed correctly because of some surrounding characters.

$test = "BOOOM";
echo "$tested";

What do you think gets output here? The answer is nothing. PHP looks for a variable named $tested, and since it doesn’t exist, nothing is output. The proper way to output the value followed by ‘ed’ would be:

$test = "BOOOM";
echo "{$test}ed";

By wrapping it within the curly brackets, it lets PHP know where the variable starts and stops, allowing it to properly evaluate the variable.

Speed Difference

I ran a few simple benchmarking tests to evaluate some difference scenarios, the difference in speed between the 2 is negligible. Single quotes was every so slightly faster due to the fact that it doesn’t have to look for a variable and then parse the value , but I have never encountered a scenario where it made enough of a difference to matter. Use whichever suits your current needs.

Posted by Will on June 29, 2012

Leave a Reply

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

TechJunkie Also Recommends