Social Question

amwelles's avatar

Is it better to use single or double quotes when coding with PHP?

Asked by amwelles (26points) February 12th, 2010

For example, should I be using <?php include(‘header.php’); ?> or <?php include(“header.php”); ?>

I’m pretty sure it can go either way, but is there one that’s preferred over the other?

Observing members: 0 Composing members: 0

6 Answers

gggritso's avatar

They’re actually interpreted differently. See here.

phoenyx's avatar

reconsiders writing a long rant about magic quotes

gggritso's avatar

@phoenyx I’ll read it!

noyesa's avatar

I’m not sure if there’s a computational advantage to using single quotes, but they won’t allow you to do string interpolation, which is what happens when variable names within strings get automatically replaced with the contents of the variable. Perhaps there’s reduced overhead, but that isn’t going to make or break your scripts.

If you’re echo-ing snippets of HTML via string, the best tactic is to alternate—if your XHTML attribute values are surrounded by double quotes, then put your string in single quotes.

The difference is entirely personal. I work in many languages, so I prefer double quotes. It’s more of a self discipline thing. Single quotes and double quotes are not the same thing in many other languages, like C++, and I prefer the consistency. My first language is C++, so in my head, single quotes delimit char data types and double quotes delimit strings (std::string or C-strings).

However, in many scripting languages, like PHP, there is almost no difference other than the aforementioned string interpolation. If you’re looking for a definite standard, the Zend Coding Standard uses single quotes unless you need to use double quotes. Using single quotes allows you to put double quotes within a string without escaping them, that’s the only advantage.

Vincentt's avatar

I mostly do single quotes because it’s marginally faster (since it doesn’t have to parse variables and whatnot), but it’s really insignificant (i.e. it’s definitely not worth it to change all your existing code to use single instead of double quotes), so it kind of comes down to personal preference. Though mostly I adhere to a coding standard (e.g. PEAR’s) for consistency and readability by other developers, and in that case often single quotes are required unless you need one of the parsing features of double quotes.

Answer this question




to answer.
Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
Knowledge Networking @ Fluther