Do Single-Quoted Strings Cause More Harm Than Good in PHP?

When writing PHP code, is it better to use single or double quotes? Using single quotes wherever possible will improve the performance of your code, but does it cost too much in productivity when less experienced developers work with the code? After all, certain things won't work as some might expect when using single quotes, such as variables inside the string.

Graphic which says 'Single' vs "Double"Before getting to the question at hand, let's look at the difference between single and double quotes.

Background

In PHP, quotes are used to indicate that something is a string. For example, the content between the double quotes below is considered a string.

print "PHP is very useful for displaying content to the screen. If you make websites, PHP may be a great solution for you.";

Well, you may have noticed that there were a few keywords throughout the string. Without the quotes, PHP would have a difficult time knowing what do with the "if" for example. Should it be a string of an if() construct? So we use quotes around the string to prevent PHP from throwing an error.

Also, the string could have been surrounded with single quotes and it works as expected. However, if we plan to add something to the string that needs to be interpreted by PHP, such as a variable, we need to use double quotes.

$customer_name = 'Sam';
print "Hello $customer_name";

Using single quotes in the above print statement would display "Hello $customer_name" instead of "Hello Sam".

Why Single Quotes Are More Efficient

If double quotes allow us to do more with strings, why would we ever using single quotes? Well, the thing about double quotes is that PHP needs to look at everything inside the string before displaying it; whether or not the string contains variables. Single quotes on the other hand, are displayed as is. PHP doesn't really look at what's inside requiring less processor time.

Of course, we most likely won't notice the difference between using single vs. double quotes since the effect is minimal, unless we were working with millions of strings.

Problem with Single Quotes

So with the differences between single and double quotes in mind, let's talk about the issue alluded to earlier with using single quotes. If our program has dozens or hundreds of lines which go back and forth between single- and double-quoted strings, what happens when someone adds a variable or some other character which doesn't work from within single quotes like the new line character ("\n")? Well naturally, that content won't display as expected.

The fix for the problem may not be apparent if they've been adding the content in question elsewhere and it works just fine. We've all probably had one of those days where things don't quite click. We may search hours because we're looking at the wrong thing. Maybe we're trying to figure why a variable doesn't contain data so we trace it throughout the program. But in the end, we discover that variable name was misspelled.

Conclusion

So is it better to avoid a potential pitfall…or do we opt for keeping the code streamlined? In the end, that's a question that needs to be answered by you and/or your team. For what it's worth, I typically prefer keeping code as clean as possible. So using single quotes when possible is my option of choice.

0 Comments

There are currently no comments.

Leave a Comment