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.

However, I think your first example is more efficient from a maintenance/understanding point of view. The blue coloured variables stand out easily so it's plain to see where they are used - when they are disguised in the string it is much harder to find them. This is more apparent when working with unfamiliar code.

I do whichever will result in me having to do the fewest escapes or closings. I used to put everything in single quotes and break to include variables, but now I only do that if I have a lot of double quotes in the string. The fewer backslashes and closing quotes the better as those are where most of my syntax errors end up being introduced.

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}.";

You are talking about such tiny time differences here that the efficiency argument becomes ridiculous. As I said above, if you are coding everything to be efficient instead of readable, you are doing it wrong.

Logic has little to do with it, you need to know how the parsing is done and how faster/slower it is as compared to concatenation. My logic somehow tells me the parsing is more expensive.

But as already been said in this thread, I don't fall for such minutiae and use what requires less typing and is easier to read. Single quotes for plain strings. Double quotes with variables in them. And on those rare occasions of outputting html directly, heredoc.

I tend to use single quotes more because there are some rare occasions that I have to use HTML inside a printed string (like an XML declaration or using quotes in email bodies). Using single quotes means I don't have to escape all the quotes for the HTML attributes.

Also, concatenating strings with variables is known to be faster than embedding variables inside strings. Either way, it's still a bunch of variables separated by small strings to PHP. Your 5 vs 1 argument doesn't hold much water. When you embed the variables, the same thing is happening, only it's PHP doing all the work instead of you. PHP has to parse the string to replace all the variables instead of performing string concatenation.

Regardless, just write the strings the way that works best for you and don't worry about performance. Performance of something like this is going to be extremely negligible anyways.

I didn't say that it makes a noticable difference, but that surely the creation of 5 single strings is negligably slower than one single string? I'm talking in steps of processing at depth - the fact that you are starting and ending 5 separate strings and loading them into memory.

I know that single strings don't really make things much faster.

However, I still use them unless I have a variable or in the string, or if it contains more than one line. For that I use HereDoc.

Not for speed reasons, but both standards and ease-of-reading - especially with quoted HTML attributes.

Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona

Typically, if it's a static string I try to remember just to use single-quotes, since it probably gives some infinitesimally small performance gain.

I then use double-quotes whenever I have a string that needs a variable (like echo), though sometimes I will do single-quotes and concatenate the variable with a dot.

I'm moving towards the latter and away from the former.

As for use the Heredoc causing indenting trouble, I will usually write a function that can take out the extra tabs. Since I'm usually fairly consistent with my tabbing, if I usually remove x-number of tabs on each line with my function, it works probably. Also, another thing I found that really helps is if you put a blank newline at the end.

Single quotes are apparently faster, although I have nothing to back this up. I heard it long ago (with a reason), so I simply started using single quotes. Now it's just a habit and I find that it looks nicer. It helps to separate variables and text in a very organised fashion.

I use single quotes on mondays, wednesdays and fridays, double quotes on tuesdays and thursdays. When combined with my variable naming and tab usage conventions I can look at code and figure out which day of the week it was written on. It's then just a simple matter of looking up my diary to see what it actually does.

It might depend on the syntax highlighting capabilities/style of your editor.

For instance, some might highlight variables within double quoted strings, and some may not.

Mine does, so it's easy to spot them and I do use double quoted strings with variables in them. I usually don't need to do 'This ' . $this . ' this', because "This $this this" is easier to type and read.

Some people say single quotes are faster to parse. This was true a long time ago, but it isn't true now as PHP is no longer interpreted line-by-line (a single line of code is compiled once, run many times). With a PHP accelerator, the difference is eliminated completely. Also, IMHO over-optimisation is a very bad thing in programming. In almost all situations, optimisations will make a negligible difference if any, and your priority should be code readability. Optimising should be on certain highly-used sections of your code only.

Programming conventions is something we just started taking a serious look at and initiating in all of our code. The idea is to create a standard all across the board in every aspect of web development code for HTML, CSS, Javascript, and PHP. This way, it makes the code a LOT cleaner and it's easier for other developers to read our code. There are a few key points to our system:

- We no longer abbreviate variables. From time to time we'll use $i as a counter, but instead of using $k in a foreach for the key, we'll be descriptive and use $key.
- Everything is camelCase. All of our function names, variables, everything is done in camelCase. Where the first letter of each word is capitalized except the first word. So for the function thisIsCamelCase() you can clearly see how it's done.
- We always use double quotes when defining variables. It's a lot less likely you'll cause a syntax error when typing in the string. In the end, it saves you from having to escape any ' which are much more commonly used.
- Part of our new transitions is to use smarty. So all of our variables are called using {$variable}. However in the instance we're defining a variable that will go into Smarty we would go ahead and do"

Code:

$movieTitle = "The Day After Tomorrow";
echo "Today we're going to see the movie ".$movieTitle." after lunch.";

For the people talking about the speed difference between single and double quotes, http://phpbench.com indicates there is no significant difference between those two. I think you're wasting your time if you go around and change quotes to gain performance. Your script will probably have other things you can optimize instead.

To answer the question. I always use single quotes unless I need some special char like \t which has to be in double quoted strings (or heredoc) seeing as single quotes strings are verbatim.

It claimed that while() loops were faster at looping through arrays than foreach - of course, this isn't true in versions of PHP above 4 - but of course they use an old server.

As for special characters like \r, \n, \t etc - I sometimes keep those in constants. That way if I want to properly see the output, I just replace the "\n" with '<br />' and voila.

I never use double quoted strings unless the string has a variable in or a newline etc. I just plain don't like using them - and whilst the difference may be neglegible, the fact is that it is less efficient and there's no point if you're not going to use vars inside of the string.

Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona