Strings in PHP can be specified in four different ways: single quoted, double quoted, heredoc syntax and (since PHP 5.3.0) nowdoc syntax, the first two of them being by far the most frequently used.
It is important to know the difference between using single quotes and double quotes. In this post we will see the difference between them and which should be used when.
Single quoted strings are the easiest way to specify string. This method in used when we want to the string to be exactly as it is written. When string is specified in single quotes PHP will not evaluate it or interpret escape characters except single quote with backslash (‘) and backslash(\) which has to be escaped.
echo 'This is \'test\' string'; //Output: This is 'test' string
In double quoted strings other escape sequences are interpreted as well any variable will be replaced by their value.
$count = 1; echo "The count is $count"; //Output: The count is 1
If we use single quotes instead of double quotes for the above example it will be like this:
$count = 1; echo 'The count is $count'; //Output: The count is $count
I recommend using single quotes (‘ ‘) for string unless we need the double quotes (” “). This is because double quotes forces PHP to evaluate the string (even though it might not be needed), whereas string between single quotes is not evaluated. Also, parsing variables between strings takes more memory than concatenation.
So instead of this:
$count = 1; echo "The count is $count";
$count = 1; echo 'The count is ' . $count;
20 thoughts on “PHP double quotes vs single quotes”
Pingback: PHP Character Conversion? | Seek-Answers
Very informative! I’ve been using PHP for over 5 years and never bothered looking into the differences between single and double quotes.
Okay you just made my with this.
I knew there was the whole “$var”=value system, but I didn’t know that ‘quotes’ are more efficient since they need less memory and work. Thatnks a lot I already rewrote our page and finally I don’t need to break the string each time I need to put the same type of quote inside itself. 🙂
But I’d do like to ask one thing. I had a few errors when I changed everything and then I learned that things like URL-s need to be between “quotes”(I just finished my university in informatics, but minor things like these were left out in class).
So I’ds like to ask if “quotes” are used within ‘quotes’ those are not checked by PHP, but used by HTML in the browser right?
Because then some of the inner ‘quotes’ could be changed to “quotes” instead of escaped by \. At least at parts where ‘quotes’ aren’t required.
Yes, you can use “quotes” withing ‘quotes’ and they won’t be checked by PHP.
Great. Thx for the help. 🙂
I’d recommend interpolation over concatenation for two reasons:
1. It’s more readable
2. It’s less error-prone. In your example above you made this exact error, since the two strings are not equivaltent – there’s a space missing in the concatenation version.
Thanks eik3. I have updated the post.
thanks. i got the differents between single quotes and double quotes in php
Which is faster? Single quote or double quote?
Single quotes should be faster than double quotes. Unless you have very complicated string with lots and lots of concatenations. But in most cases single quotes will be faster.
I’m not sure if winning a micro second is really what we can call faster 😉
We might be saving only a micro second in small applications or when we don’t have lot of statements with double quotes. However, in large applications or when using lot of similar statements, we can potentially save lot of micro seconds or even a few seconds. Also, when we want to have our application be as fast as it can even a micro second can help.
Also note, that a string like this:
$mailbody = ‘I want a line break:\nDone.’; // is parsed showing the \n
$mailbody = “I want a line break:\nDone.”; // will give you the line break
A mistake that actually everybody is running in, once in a while 🙂
Thanks for putting this together. I always use single quoted because on my keyboard (swiss-german) it has its own key compared to double-quoted where I need to press shift+2. I only learned later the differences between those.
Thanks for this post. I have been struggling with trying to use store and retrieve and echo an apostrophe (which is a single quote) and after searching high and low for a clue as to the problem, I happened upon your post which made clear what my problem was. Thanks for being a lifesaver.
Thanks for this awesome post.
This is very informative! Thank you!
Getting very useful to me…