Difference between single-quoted and double-quoted

5/5 - (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 of apostrophes.
To use the apostrophe in single quotes, we have to put a backslash before apostrophe (which is named as an 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.

Why?

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.

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment

WebPedia.net