Difference between single-quoted and double-quoted

Difference between single-quoted and double-quoted
5 (100%) 1 vote

The most important difference between single and double quote, is that anything enclosed in single quotes is treated as literal text.

Single quotes

When using single quotes we must take care with apostrophes.
To use the apostrophe in single quotes, we have to put backslash before apostrophe (which is named as escape character).
The backslash tells the PHP to treat the following character as part of the string.

echo 'Mike\'s first exercise'; //Result: Mike\'s first exercise
echo '"Mike\'s first exercise"'; //Result: "Mike\'s first exercise"

Therefore you do not get the value of a variable in single quotes, you will get the variable itself.

$name = "Mike";
echo '$name first exercise'; //Result: $name first exercise


Double quotes

To get the value of a variable in a string in PHP we must use double quotes.

$name = "Mike";
echo "$name first exercise"; //Result: Mike first exercise

This will lead to a decrease in performance when we use double quotes.


When we use double quotes, PHP have to parse to check if there is any variables in there, which will inevitably lead to a drop in performance.
In conclusion it’s faster to use single quotes in PHP.

Escape sequences

The most common escape sequences that are used in PHP:

\n New line
\r Carriage return
\t Tab
\\ Backslash
\$ Dollar sign

The only escape sequences that works in single quoted strings are: backslash apostrophe or backslash single quote.
All other escape sequences must be in double quotes.

Leave a Reply

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