Categories

Articles in Basics Category

Usage of Quotes

In PHP, single and double quotes are used to enclose strings. Following code would throw you a PHP error since the string hasn’t been enclosed by quotes.

$name = Robin Jackman;

It has to be enclosed by quotes like below.

$name = 'Robin Jackman';

While both single and double quotes can be used to enclose strings, there is a difference in how PHP interprets two methods. If you used double quotes then PHP would interpret variables and special characters (\t, \n etc) inside the string but if you used single quotes PHP would consider only the literal values of the whole string.

$name = 'Robin Jackman';
echo "Name is $name"; // Would print Name is Robin Jackman
echo 'Name is $name'; // Would print Name is $name

It’s better that you use double quotes only when you want to pass variables and special characters and use single quotes when you only want to pass pure strings like $name variable in above examples.

The reason is having double quotes requires PHP interpreter to check for variables and special characters inside the string which is an extra step. Even though time it takes for this is trivial, you should consider writing optimized code as much as possible.

Share with Your Peers ...

Where to Head from Here...
We Value Your Feedback...

We love to hear what you think about this article. Please provide your opinion, suggestions and improvements using following form. Note that submitted feedback is not displayed but we will get back to you if it needs a reply.