Single quote, double quote, or no quote? This seemingly simple question is often asked by those new to PHP. Suprisingly, however, some of those who claimed to be PHP programmer also can’t tell the difference. It’s very simple, really.
The main difference
Both single and double quotes are used to define string literals in PHP. Technically, you can use either one for simple strings.
<?php echo 'A simple string'; echo "A simple string"; ?>
What’s the difference then? With single quotes, PHP will display every character between the opening and closing quote as literal character. If you want to display single quotes within the string, use backslash character to escape them. On the other hand, using double quotes to enclose a string will cause PHP to parse the string. This means PHP will expand variables and escape sequences for special characters. Take a look at the examples below:
<?php $name = 'John Doe'; // Output: My name is $name echo 'My name is $name'; // Output: My name's $name echo 'My name\'s $name'; // Output: My name is John Doe echo "My name is $name"; // Output: My name's John Doe echo "My name's $name"; ?>
When to use which?
After learning the difference between single and double quotes, it should be easy to answer the above question. If you don’t have variables inside your string, use single quotes. If you have variables, do concatenation using dot character. This practice reduces the time used to parse your scripts because concatenation is faster then having PHP to parse every string enclosed in double quotes. The difference might be small, but every microsecond counts when you have very busy sites.
As this is not a hard rule, it’s ok to violate it once in a while when it makes more sense to you. Sometimes I use double quotes to write SQL statements because SQL statements often contain many quotes and using double quotes to enclose the whole statements makes them easier to read.
<?php $sql = "SELECT * FROM table WHERE column1 = '$value1' AND column2 = '$value2' AND column3 = '$value3'"; ?>
Is that all?
No. Another case where I often see misuse of quotes is when working with associative arrays. Suppose we have an associative array as shown in the example below, which is the correct way to display its array elements?
<?php // This is our array $fruits = array( 'name' => 'banana', 'color' => 'yellow' ); // Should we use single quotes? echo $fruits['name'].' is '.$fruits['color']; // Double quotes? echo $fruits["name"].' is '.$fruits["color"]; // Or no quotes? echo $fruits[name].' is '.$fruits[color]; ?>
I hope by now you can correctly answer that using single quotes is the way to go. Why? Firstly because our array keys are strings and therefore we need to enclose them in quotes. Secondly, the keys are simple strings. The last echo in our example above will make PHP looks for constants named ‘
name‘ and ‘
color‘. If you don’t have constants named ‘
name‘ or ‘
color‘, PHP will correctly guess that you mean to put string there and display the array values. What will happen if we have the following? I’ll leave it up to you to try 🙂
<?php // Now we have a constant named 'name' define('name', 'abc'); // This is the same array $fruits = array( 'name' => 'banana', 'color' => 'yellow' ); // This should print correctly echo $fruits['name'].' is '.$fruits['color']; // This too (although not a very good practice) echo $fruits["name"].' is '.$fruits["color"]; // What will be printed here? echo $fruits[name].' is '.$fruits[color]; ?>
As rules of thumb:
- Use single quotes and concatenation all the time. It makes life easier especially when printing associative array elements. There are other ways to print associative array elements inside a string enclosed with double quotes, but concatenation is way easier.
- Violation of the above rule is not a crime, but do it sparingly.
- Use single quotes to enclose array keys.
- Do not use quotes if you only want to assign one variable to another.
<?php // The quotes are not needed here $a = "$b"; // You can do the same without them $a = $b; ?>