SitePoint Sponsor

User Tag List

View Poll Results: Single quotes or double quotes?

Voters
90. You may not vote on this poll
  • Single quotes

    45 50.00%
  • Double quotes

    11 12.22%
  • Mixed

    34 37.78%
  • Neither

    0 0%
Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 72
  1. #26
    SitePoint Evangelist ferrari_chris's Avatar
    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.

  2. #27
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    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.
    Then you need to get a better IDE.

    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

  3. #28
    SitePoint Evangelist ferrari_chris's Avatar
    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.

  4. #29
    SitePoint Enthusiast
    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.

  5. #30
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Well, techincally no.

    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}."
    5 smaller single quoted strings concatenated with vars, or one double quoted string with vars inside.
    5 creations vs 1. Think about it.
    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.

  6. #31
    SitePoint Member
    Join Date
    Sep 2008
    Location
    florida
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will try that some day

  7. #32
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    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.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  8. #33
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #34
    Django Jedi neron-fx's Avatar
    Join Date
    Sep 2007
    Location
    Bristol/Bath, UK
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Single quotes for plain strings. Double quotes with variables in them. And on those rare occasions of outputting html directly, heredoc.
    This is exactly what I do
    Neron-Fx
    Everytime a user opens Internet Explorer, a web developer dies...
    http://www.savethedevelopers.org/

  10. #35
    PHP/Rails Developer Czaries's Avatar
    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.

  11. #36
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    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 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

  12. #37
    SitePoint Wizard
    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;
    Then concat onto that:
    [code]
    $html .= <<<EOL
    ...some more code...
    EOL;

    It results in:
    ...some code......some more code...
    Which usually isn't the desired effect.

    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;
    Gives you:
    ...some code...
    ...some more code...
    The heredoc method is what I use anytime I need to do anything with multiple lines (like HTML).

  13. #38
    Pedantic Semantic blain's Avatar
    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.

  14. #39
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    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

  15. #40
    is craving 'the potato' slayerment's Avatar
    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.

  16. #41
    SitePoint Guru rageh's Avatar
    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.
    ------------------

  17. #42
    SitePoint Wizard
    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.

  18. #43
    SitePoint Wizard cranial-bore's Avatar
    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.

  19. #44
    One website at a time mmj's Avatar
    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

  20. #45
    SitePoint Guru
    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.";

  21. #46
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by charmedlover View Post
    Single quotes are apparently faster, although I have nothing to back this up.

    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 &nbsp;&nbsp;= ' . ($finish-$start);
    ?>
    Results

    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

  22. #47
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,098
    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
    When you view the html source it is not just one long string, at least thats how I have been doing it.
    What I lack in acuracy I make up for in misteaks

  23. #48
    SitePoint Wizard gRoberts's Avatar
    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.


  24. #49
    SitePoint Evangelist
    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.

  25. #50
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •