New Website

I've made a new website, as lynx.io is dead. You can find it here: http://macr.ae/—it's similar in format to lynx.io, but has better articles, and they're all written by me.

JavaScript disabled

While it will still mostly work, a lot of this site's functionality relies on JavaScript - please enable it for the best experience.

Rumour Debunked: Single quotes vs double quotes in PHP

There is a popular rumour that while writing PHP, you should use double quotes wherever possible. This used to be true - before PHP 5.1 was released, there was a significant difference in speed between double and single quotes, and it was definitely a good idea to use single quotes wherever possible. PHP 5.1 included some optimisations to the opcode parser which improved the double quotes parsing time, and since then, using double quotes hasn't been an issue.

I wrote some simple benchmarks to prove my point:

<?php

$max = 1000000;
$output = '';

ob_start();

$starttime = microtime(true);

for ($i = 0; $i < $max; $i++) {
        echo "test";
}

$output .= 'Test one: ' . (microtime(true) - $starttime) . 's' . PHP_EOL;

$starttime = microtime(true);

for ($i = 0; $i < $max; $i++) {
        echo 'test';
}

$output .= 'Test two: ' . (microtime(true) - $starttime) . 's' . PHP_EOL;

ob_end_clean();

echo $output;

I got the following results:

Test one: 0.070215940475464s
Test two: 0.070168018341064s

As you can see, the difference is negligible - an average of 0.0479 nanoseconds per test. In comparison, some other common tasks take the following amount of time on my computer:

Check out http://www.phpbench.com/ for some other examples of times.

 

So in summary, it doesn't really matter whether you use single of double quotes as it has no real effect on speed. The only reasons you would use double quotes over single quotes is for aesthetics or for if your project still supports PHP versions less than 5.1.

About Callum Macrae:

Callum Macrae is the founder of lynx.io and a JavaScript developer from the United Kingdom. He is currently writing his first book, to be published by O'Reilly Media.

You can view more articles by this author here.

Tags: php

Comments

TheMaster says:

There’s not only the speed part of it.

Double quotes allow for variables to be output, whereas single quotes merely output litterally what in between them.

So with longer strings, if you use double quotes PHP will esentially be “searching” the string for a variable, which takes more time the longer the string it, as opposed to merely outputing a single quoted string.

But IMHO the speed difference is negligable and I noramally use double quotes everywhere, except when I can’t (quotes within quotes). :D

Callum Macrae says:

No, PHP won’t search the string – that isn’t how the parser works.

Alex says:

First of, you mix up single and double throughout this post.

Doubles used to be slower, and always will be as they support escape sequences and variable expansion. The parser may have been improved, but it will always have more work per character in double quoted strings. Not that it really matters. What matters is that single quoted strings don’t have variable expansion. Variable expansion can be overlooked or ambiguous. You could call it a matter of “aesthetics”, but it is actually a matter of clarity.

Besides your conclusion, your benchmark is completely wrong, on so many levels. It should be obvious that your string operation is so small compared to the overhead of your loop, the difference between single and double quotes would never register. And since the speed issue is mostly a parsing issue, you’re not even measuring right. (There are other things wrong with this “benchmark” but not relevant to the single vs double quotes debate.)

Oliver says:

If you were to put code like this:

The first line would output “def” (parse the variable) whereas the second line would output “$abc” (doesn’t parse the variable name)

Callum Macrae says:

Yes. But it doesn't "search" the string, it's a lot more efficient than that.

says:

Add comment

 

You can use markdown in comments (press "m" for a cheatsheet).

Enable JavaScript to post a comment

Markdown Cheat Sheet

# This is an <h1> tag
## This an <h2> tag
###### This is an <h6> tag

Inline markup: _this text is italic_, **this is bold**, and `code()`.

[Link text](link URL "Optional title")
[Google](http://google.com/ "Google!")

![Alt text](image URL)

![This is a fish](images/fish.jpg)

1. Ordered list item 1
2. Ordered list item 2

* Unordered list item 1
* Unordered list item 2
* Item 2a
* Item 2b

And some code:

// Code is indented by one tab
echo 'Hello world!';

Horizontal rules are done using four or more hyphens:

----

> This is a blockquote

This is an <h1> tag

This an <h2> tag

This is an <h6> tag

Inline markup: this text is italic, this is bold, and code().

Link text Google

This is a fish

  1. Ordered list item 1
  2. Ordered list item 2
  • Unordered list item 1
  • Unordered list item 2
    • Item 2a
    • Item 2b

And some code:

// Code is indented by one tab
echo 'Hello world!';

Horizontal rules are done using four or more hyphens:


This is a blockquote

Toggle MarkDown / HTML (t), full reference or close this