The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

The result of all three statements is the same, but the interpreter doesn't see it thay way. The last one (print test is definitely the worst, because the compiler will first have to check if 'test' is a defined constant, and only after it figures out this isn't the case, will it print the string "test".
The first example (print "test" also isn't great, because the interpreter has to scan the whole string for occurrences of variables. For this short string this isn't any problem, but for larger strings it might become noticeable.

For these reason I personally ALWAYS stick to single quotes.
What do you use?

I mean every single line of HTML. It seemed flawless because it was a discipline I could remember, it was simple and it worked. I rarely come across remaining old code, but sometimes I do, and I groan. Still, not having to debug my print statements left me free to debug everything else I was doing wrong.

Slowly I started to use echo and single concatenated quotes, mostly after reading posts on this subject on this forum.

echo 'My name is ' . $name ;

I found this made my vars easier to spot, and was much influenced by the fact I was learning OOP - which generally speaking, should return data, not echo or print output directly.

I went down the road of comma separated echos for a while:

echo 'My name is ', $name ;

Which is even faster ... wheeee ....

But, I found it harder to be consistent, so I stopped using that.

Then of course Heredoc came out, so monster blocks of code including Javascript (wow!) could finally be injected into your page as is.

So given that I am now layering my applications, generally speaking, not outputting much html directly, I allow myself the freedom to use both single and double quotes as and when the situation suits me. for example a pile of vars to go in a string:

echo "The $size $feline $posture on the $product." ;

( the fat cat sat on the mat )

rather than

echo 'My name is ' . $name ;

Maybe my "readability imperative" is now to do with the flow of data rather than the html.

Choosing single or double quotes is often governed by where its headed, if its back into an Ajax-driven DOM then its often singles inside doubles to minimise all the inevitable backslashing crap.

So when starting out pick one method and stick to it for a while. Train your editor to create the text with a shortcut key.

But when you gain confidence and knowledge, experiment, and don't be afraid to use both/ether.

Eventually this becomes a non-issue as you tend to output very little to the the page, or that output goes into some kind of placeholder, such as a template.

I use Single quotes and of course the variables outside of the quotes but until I go for HeareDoc or NewDoc. Single quotes doesn't take compiler to take care of variable interpolation as far as I know.

I want to start using Heredoc, but I try not to output large blocks of HTML via php. For large blocks I switch it up a bit and exit out of the parser (this is only for the template layer and never inside of a function):

I always use double quotes and always refer to the variable outside the quotes. As far as I know, it's the same basic fail-safe method across PHP, Java, JavaScript, and C#

Likewise. I do tend to switch between double and single - I use single for 'simple' text things like constant names in define() etc. Mostly double though, and I never embed variables within it. I don't use heredoc either - I will switch out of PHP instead.

Anyone using efficiency as a reason is doing it wrong! Readability and maintainability is FAR more important than efficiency/speed. The differences are absolutely minute, and you shouldn't code for efficiency except in the most extreme examples, you will end up making your code look awful / nightmare to maintain.

Despite several attempts at trying to work it out, I never ever saw the point of sprintf() or why you should use it.

Logically thinking, creating many single quoted strings would technically be less efficient than one larger double-quoted string with vars in.

i.e:

PHP Code:

$single = 'hello my name is '.$name.' and my age is '.$age.'. I live in a '.$building.' in '.$location.'.';
$double = "hello my name is {$name} and my age is {$age}. I live in a {$building} in {$location}.";