Friday, 20 September 2013

PHP double quotes vs single quotes

This is something I've been unsure about for quite a while but as it's such a minor thing really I haven't actually looked into it or given it much thought. Generally when using quotes to output a string in PHP should you use single quotes or double quotes?

Turns out in my opinion double quotes are best used most of the time, here's why.

Double quotes

With these you can put variables directly into your string and it'll display the value of that variable without the use of any concatenation. I've noticed that generally in my PHP programming when I'm putting variables in a string to be output to the browser I want the actual value of it to be displayed, not the name of the variable itself.

Single quotes

Every thing contained within these is basically displayed as it's written, for example if you put a variable in the middle of the string (without any concatenation) this'll output the name of that variable to the browser but not the value of the variable.


I thought rather than writing to much above about them I'd have a table of examples instead as trying to explain it would require some 'jargon' and depending on how well you understand programming it could become hard to understand what's going on.

For this example $test = 'Neil'; and $test[0] = 'Nand';
PHP codeOutput
Single quote
echo '$test is so, so great';$test is so, so great
echo '$testArray[0] is so, so great';$testArray[0] is so, so great
Double quotes
echo "$test is so, so great";Neil is so, so great
echo "$testArray[0] is so, so great";Nand is so, so great

Something to note

Something some of you may have noticed in my examples table that when using double quotes if you tried to echo out the string "$tests so, so great"; then PHP would look for the variable $tests (which doesn't exist) rather than $test.

Stopping this happening is easy, when you put a variable in the string surround it with {}, to correct the example about it'd be echo "{$test}s so, so great";

What is concatenation?

I mentioned concatenation earlier on in this post, encase anyone wasn't sure what it means I've put two examples below. The first is without concatenation (which will display the variable name and not its value) and the second is the same statement written with the variable & string concatenated so it'll display the variables value.
echo '$test is so, so great";
echo $test . 'is so, so great';


  1. Here at PHP-Programming-India we guarantee your business development and expanded deals and distinction by utilizing most recent highlights in the business and best coding practices. Top Expert Cakephp Developers

  2. Generally, everything that is promptly, can be taken, and it is the equivalent for PHP source code that can be taken by your own clients or other web engineers and to put it in various exercises paying little mind to the likelihood that you went with the primary course of action or thought.Why use Laravel