SitePoint Sponsor |
|
User Tag List
View Poll Results: Single quotes or double quotes?
- Voters
- 90. You may not vote on this poll
Results 26 to 50 of 72
Thread: Quoting strings: what do YOU do?
-
Sep 30, 2008, 15:48 #26
- Join Date
- Mar 2008
- Location
- NP, New Zealand
- Posts
- 576
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Efficient from a processing point-of-view, maybe.
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.
-
Sep 30, 2008, 16:13 #27
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
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.
phpDesigner2008 and NotePad++ highlight variables inside strings, including heredoc.Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Sep 30, 2008, 17:17 #28
- Join Date
- Mar 2008
- Location
- NP, New Zealand
- Posts
- 576
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Oh OK. DreamWeaver doesn't do that.
-
Sep 30, 2008, 22:22 #29
- Join Date
- Sep 2006
- Posts
- 56
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Oct 1, 2008, 01:34 #30
-
Oct 1, 2008, 04:20 #31
- Join Date
- Sep 2008
- Location
- florida
- Posts
- 16
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I will try that some day
-
Oct 1, 2008, 04:57 #32
- Join Date
- Jan 2004
- Location
- The Kingdom of Denmark
- Posts
- 2,702
- Mentioned
- 7 Post(s)
- Tagged
- 0 Thread(s)
Since I practically never use the dollar sign in normal texts, I always use double quotes with the variables inserted outside the quotes.
-
Oct 1, 2008, 05:27 #33
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.
-
Oct 1, 2008, 06:47 #34
- Join Date
- Sep 2007
- Location
- Bristol/Bath, UK
- Posts
- 274
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Neron-Fx
Everytime a user opens Internet Explorer, a web developer dies...
http://www.savethedevelopers.org/
-
Oct 1, 2008, 07:37 #35
- Join Date
- May 2004
- Location
- Central USA
- Posts
- 806
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.Stackbox CMS - Full edit-on-page drag-and-drop CMS
Autoridge - Vehicle information & maintenance part numbers
Twitter | Blog | Online Javascript Compressor
-
Oct 1, 2008, 08:27 #36
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
I was referring to Stef686's point:
singles are faster
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
-
Oct 1, 2008, 09:26 #37
- Join Date
- Dec 2003
- Location
- USA
- Posts
- 2,582
- Mentioned
- 29 Post(s)
- Tagged
- 0 Thread(s)
As far as the original question, I usually vary a bit.
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.
So, for example, say you have:
Code:$html = <<<EOL ... some code... EOL;
[code]
$html .= <<<EOL
...some more code...
EOL;
It results in:
...some code......some more code...
If you put an empty newline at the end, it'll start from that point instead, so:
Code:$html = <<<EOL ...some code... EOL; $html = <<<EOL ...some more code... EOL;
...some code...
...some more code...Xazure.Net - My Blog - About Programming and Web Development
Follow Me on Twitter!
Christian Snodgrass
-
Oct 1, 2008, 12:44 #38
- Join Date
- Mar 2006
- Location
- Yorkshire, UK
- Posts
- 528
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code PHP:$foo = "blain"; printf ("Why has noone mentioned this format? Ponders %s", $foo);
Technology is dominated by two types of people:
those who understand what they do not manage,
and those who manage what they do not understand.
-
Oct 1, 2008, 13:07 #39
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Posts 4 and 6
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Oct 1, 2008, 15:29 #40
- Join Date
- Nov 2002
- Location
- Scottsdale, Arizona, USA
- Posts
- 603
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I use whichever one requires the least amount of escaped characters. So if I have a lot of html w/ doubles then I'll use singles. If I have js with a lot of singles I'll use doubles.
-
Oct 1, 2008, 15:50 #41
- Join Date
- Apr 2006
- Location
- London, Formerly Somalia
- Posts
- 612
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I must say I use mixture of them, but rarely do I use HEREDOC.
------------------
-
Oct 1, 2008, 19:17 #42
- Join Date
- Jul 2004
- Location
- Minneapolis, MN
- Posts
- 1,924
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Oct 1, 2008, 19:24 #43
- Join Date
- Jan 2002
- Location
- Australia
- Posts
- 2,634
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Oct 1, 2008, 19:32 #44
- Join Date
- Feb 2001
- Location
- Melbourne Australia
- Posts
- 6,282
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
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.[mmj] My magic jigsaw
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The Bit Depth Blog · Twitter · Contact me
Neon Javascript Framework · Jokes · Android stuff
-
Oct 2, 2008, 05:56 #45
- Join Date
- Sep 2004
- Posts
- 613
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.";
-
Oct 2, 2008, 07:04 #46
- Join Date
- May 2005
- Location
- S.W. France
- Posts
- 2,496
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Well let me back that up, heres a script that runs the same code X number of times once each for " & '
PHP Code:<?php
// Testing speed of single quotes and dounle quotes
$string=" -please insert this text- ";
$start=microtime(TRUE);
for ($x=1;$x<50000;$x++){
$test="The quick brown $string fox jumped over $string the lazy dog. The quick $string brown fox jumped $string over the lazy dog.";
}
$finish=microtime(TRUE);
echo 'Time for double quotes = ' . ($finish-$start) . '<br />';
$start=microtime(TRUE);
for ($x=1;$x<50000;$x++){
$test='The quick brown ' . $string . ' fox jumped over ' . $string . ' the lazy dog. The quick ' . $string . ' brown fox jumped ' . $string . ' over the lazy dog.';
}
$finish=microtime(TRUE);
echo 'Time for single quotes = ' . ($finish-$start);
?>
setting x at 50,000 and running a few times I got increases in speed of between 8 - 24% by using single quotes.A Little Knowledge Is A Very Dangerous Thing.......
That Makes Me A Lethal Weapon !!!!!!!!
Contract PHP Programming
-
Oct 2, 2008, 07:28 #47
- Join Date
- Feb 2005
- Location
- was rainy Oregon now sunny Florida
- Posts
- 1,104
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
They both have their place.
PHP Code:echo 'My name is '.$name."<br> \r\n I live at ".$address;
What I lack in acuracy I make up for in misteaks
-
Oct 2, 2008, 07:40 #48
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I use a mixture of sprintf and double quotes. I'm a .net programmer by trade so i'm used to using String.Format which is a close replica of sprintf.
-
Oct 2, 2008, 08:34 #49
- Join Date
- May 2006
- Location
- Denmark
- Posts
- 407
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Oct 2, 2008, 09:10 #50
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
PHP bench isn't that reliable from what I recall.
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
Bookmarks