You are here:Home»KB»Programming»PHP»Single Quotes vs Double Quotes in PHP
Saturday, 28 August 2010 00:00

Single Quotes vs Double Quotes in PHP

Written by

use single quotes unless escaping characters

Article 1

I don’t write about PHP very often — this is the first time since the launch one year ago. However, I do quite a lot of work with PHP and relational databases such as MySQL. Almost every single time I review someone else’s work a common error related to using strings in PHP is made. Misunderstanding the way of defining strings in PHP is often the cause of many hours of debugging as well. If you are relatively new to programming and PHP you’ll most likely recognize this situation.

PHP lets you define strings using single quote, double quotes or heredoc. There’s a crucial difference between the first two and for some reason many people new to PHP start using double quotes. Hopefully the following explanation will save a couple of hours debugging for the starters among us.

The most important difference between the two is that a string enclosed in double quotes is parsed by PHP. This means that any variable in it will be expanded.

echo $var; // Results in the value of $var being printed
echo '$var'; // Results in the word '$var'
echo "$var"; // Results in the value of $var being printed

This means that concatenating strings can be done in two different ways as well.

$var = 'Ipsum';

echo 'Lorem ' . $var; // Results in 'Lorem Ipsum'
echo "Lorem $var"; // Results in 'Lorem Ipsum'

This possibly isn’t anything new to you, but if you are working with HTML you can output valid markup while keeping your code readable. Using double quotes in a string enclosed with single quotes requires no escaping and vica versa. Using a double quote in a string enclosed in double quotes (idem for single quotes) does require escaping, which —in my opinion— degrades readability tremendously.

$text = 'Lorem Ipsum';
$uri = 'http://www.lipsum.com';

echo '<a href="' . $uri . '">' . $text . '</a>';
echo "<a href=\"$uri\">$text</a>";

Personally, I always stick to using single quotes and the dot syntax to concatenate strings, unless I need special characters such as new lines. Whatever you use is up to you, but hopefully this heads-up cleared a lot of confusion and puts an end to unreadible code and markup using single quotes.


Not to mention... using " " where not needed, is just wasting processing, potentially even slowing down your website, if it's as big as say deviantART =P, since PHP has to try to parse something that does not need parsing...

Using single quotes where possible results in nominally faster execution, but we're talking fractions of milliseconds.


As with double quotes, you can escape single quotes in a string enclosed in single quotes as well. I agree that backlashes make it unreadable, but in a rare occassion I use it.

echo 'Hi, how are you? I\'m fine.'; // Single quotes
echo "Hi, how are you? I'm fine."; // Double quotes

Depending on the situation I might deviate and opt to use double quotes instead.


Article 2

The advantage to using single quotes is that you don't need to escape any double quotes in your code, which makes it neater.

However, using double quotes would allow you to place variables inside without having to escape them.

Code:
$variable = 'moo';
// This will work.
echo "Double Quotes. $variable";
// This wont.
echo 'Single Quotes. $variable';

I prefer using single quotes, as it makes echo'd HTML (the php code) look neater. My opinion, of course. :P


PHP Code:
$var $_POST['name']; 

is 7 times faster than

 

PHP Code:
$var $_POST[name]; 

Which method you choose is entirely personal, but I use 'apostrophes' to mark my strings instead of "quotes". The downside is that I must break my string in order to include a variable in it.

I do this for two reason. First, my code editor makes variables inserted in a string bold only if I break the string around them. This makes them easier to spot when I'm troubleshooting or testing. Secondly, it allows me to paste properly validated HTML into my echos and prints without having to escape all of the quotations.

You face a small inconvenience either way, but with my method, they are fewer in number.


Single quotes versus double quotes is a huge issue in PHP. Nobody seems to understand what sort of problems they run in to using double-quotes when they arn't needed.

When you use double quotes, you are asking PHP to parse through the string and search for possible variable matches and escaping characters (like \n).

This is obviously a slow process. Do you think it'd be faster to read a book looking for a secret clue in every 4 words, or to simply read it straight through? Same difference.

If you arn't using escaping characters, it is pointless to use double quotes unless your goal is to make your application slower. Here are a few examples to demonstrate:
 

Code:
$wtf = 'is';
$output = "My name $wtf monokrome"; // This is SLOW.
$output = 'My name ' . $wtf . ' monokrome'; // This is much more efficient.

$wtf = 'google';
  // This is the only sort of time where you ever really need double quoted strings.
$output = '<a href="http://google.com">' . $wtf . "</a>\n"; // Notice the newline(\n)?

As for the array keys being used as $_POST[name], of course that's slow. When you do that, PHP is trying to determine name as a keyword. Since that keyword doesn't exist, it automatically assumes that you are wanting 'name'. Obviously it's faster to not make PHP make that assumption.


Generally, for most scripts, you aren't going to see the script running slower with double quotes. However, when you get to the point where your code is getting more advanced and doing more things, or if you're deploying the script in a real world environment, that is when you need to cut back on double quotes.

Personally I use single quotes everywhere. Using single quotes plus concatenation is faster than double quotes every time.

Read 907 times Last modified on Wednesday, 12 November 2014 23:15