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 3 of 3 FirstFirst 123
Results 51 to 72 of 72
  1. #51
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I need to output a very small block of text (I mean VERY small), I use this method:

    Code PHP:
    echo 'This is some text with a variable ', $var, ' inside of it.';

    I almost never use that method. For most of my work I break in and out of PHP to output:
    PHP Code:
    <div>
        <a href="<?php echo $var?>">link</a>
        <p>
            <?php echo $anotherVar?>
        </p>
        <table>
            <?php
                
    foreach ($collection as $element) {
                    
    ?>
                        <tr>
                            <td><?php echo $element['index1']; ?></td>
                            <td><?php echo $element['index2']; ?></td>
                        </tr>
                    <?php
                
    }
            
    ?>
        </table>
    </div>
    I find both of these methods to be the most readable for their respective jobs.

    Of course, this whole problem is mostly one of personal taste. For those who are actually concerned about the performance of their PHP scripts at this tiny scale (in which case I'd suggest moving to another language), the PHPBench site has plenty of tests about this very issue.
    Last edited by Tarh; Oct 2, 2008 at 08:57.

  2. #52
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Chicago
    Posts
    542
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BrandonK View Post
    Code:
    $percent = 99.9;
    echo 'I use single quotes ' . $percent . '% of the time';
    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):
    Code:
    ?>
    <form action="<?php echo($form['action']); ?>">
    ...
    </form>
    <?php
    I used to escape the PHP code to write large blocks of HTML, but I found it more messy when I started using frameworks. This was the point when I changed the way I integrated PHP into the website.

    Now I try to keep the views and logic as separate as possible. All the HTML is on separate php files, and PHP is only present there as presentation logic. (like displaying content through a loop)

  3. #53
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    I like having my generated HTML have double-quotes so for the most part I use single-quotes for the PHP to achieve this...

    Code PHP:
    $output = '<p style="color:#000000;">Hello, my name is ' . $name . ' and I like cheese.</p>';

    ...but I voted "mixed" because there're always times when doing math that double-quoting makes more sense.




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  4. #54
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use single quotes wherever I can. I only use double quotes for SQL queries (and other strings containing single quotes) and when I need to include a line break or something (ie. "\n"). Even then, I'll go for single quotes and break into doubles to do the line break (ie. 'This is some text'."\n"). If suitable, I'll use single quotes to span multiple lines...

    PHP Code:
    echo 'This is some text<br />
    spread over multiple lines<br />
    this is pretty fun.'

    I never use heredoc, instead I'd rather go in and out of php like others have said. Looks cleaner, and allows for syntax highlighting and code hints in the editor you use.

  5. #55
    SitePoint Addict GeertDD's Avatar
    Join Date
    Feb 2005
    Location
    Belgium
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When working with regular expressions, be sure to use single quotes around them. This removes a layer of complexity regarding to escapes.

  6. #56
    SitePoint Member perkolate_jason's Avatar
    Join Date
    Sep 2008
    Location
    Las Vegas, NV
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always use single quotes except for building adhoc SQL statements. Easier to read code, IMO, and less keystrokes.

    For those that are concerned with performance here is a benchmark test comparing using both single and double quotes. Scroll or find "Quote Types".

    http://www.phpbench.com/
    Perkolate
    Las Vegas Web Design and Web Development
    http://www.perkolate.com
    http://blog.perkolate.com

  7. #57
    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)
    Quote Originally Posted by GeertDD View Post
    When working with regular expressions, be sure to use single quotes around them. This removes a layer of complexity regarding to escapes.
    Usually this is the case, but if you need to match against a pattern which contains a lot of apostrophes, isn't it easier to include the string in double quotes?
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog · Twitter · Contact me
    Neon Javascript Framework · Jokes · Android stuff

  8. #58
    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)
    Quote Originally Posted by perkolate_jason View Post
    I always use single quotes except for building adhoc SQL statements. Easier to read code, IMO, and less keystrokes.

    For those that are concerned with performance here is a benchmark test comparing using both single and double quotes. Scroll or find "Quote Types".

    http://www.phpbench.com/
    Wow, that's very interesting - it looks like in every case they tested, single quotes was slower than double, albeit by only a few percentage points, with the exception of the double quoted string which contained escaped dollar signs.

    I guess PHP have done some work optimising double quotes and they have now overtaken single quotes.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog · Twitter · Contact me
    Neon Javascript Framework · Jokes · Android stuff

  9. #59
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by perkolate_jason View Post
    For those that are concerned with performance here is a benchmark test comparing using both single and double quotes. Scroll or find "Quote Types".

    http://www.phpbench.com/
    Ive said it before and Ill say it again, the benchmarks on that site is not accurate. Unless the person that is running it has moved it to a dedicated server (which I dubt) it is still running on a shared account.

    And we all know how skewed benchmark results can become when something else is running in the background... Not to mention that the fastest solutions to some of the "examples" are not even there.

    Though, it is funny to see how many people that buy the infromation the site claim to provide without being critical to how it is setup and work.


    For the main question in this thread. There is no correct answer, both has its uses.

    Personally I use double quotes for queries or when I have to write something with a lot of single quotes in it. If I need to echo any kind of text, I use single quotes and commas.

    The double or single quotes are so similar in speed, that using one over the other does not matter (Though single quotes will always be faster, due to the double quotes has extra processing going on when it looks for variables inside the string).

    For heredoc and printf, both of those are slow as hell and should be avoided if possible. Though both has its uses, but its important to remember that they actually are slow.

    Also as a last tip, if you ever initiate an empty string, use: $text = null; instead of $text = ''; The difference is not that much, but it is there.

    While premature optimizing is not a good idea, minor stuff like that, which you can throw into your code without removing any readability etc I strongly suggest that you do.

  10. #60
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TheRedDevil View Post
    Also as a last tip, if you ever initiate an empty string, use: $text = null; instead of $text = ''; The difference is not that much, but it is there.
    But $var = '' much better suggests that a variable is supposed to hold a string, doesn't it?

  11. #61
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by earl-grey View Post
    But $var = '' much better suggests that a variable is supposed to hold a string, doesn't it?
    That is true, but usally your variable names should give a hint to what it contain as well, making that redundant.

    Not to mention you would notice that it should contain a string a few lines below as well (I initiate the variables right before they are needed).

  12. #62
    Kiwi Fr00t jylyn's Avatar
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    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.
    Good on you for resisting the pressure to work weekends.

  13. #63
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by earl-grey View Post
    But $var = '' much better suggests that a variable is supposed to hold a string, doesn't it?
    $var = ''; should be faster in the broader scope seeing as that variable will most likely be used in concatenation. If $var = null;, PHP must typecast it into a string every time it's needed to be one.

    I suck at explaining myself.

    Anyway, what I use is a templating engine, multiple-parameter single-quoted echo calls, and sprintf, ordered by priority.

  14. #64
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    how can we use padding in php

  15. #65
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I must say I quite like the emergence of :

    $str = null ;

    as it matches some stuff I have recently learned about the use of null in Sql.

    That link contains rudy comments on the ugly use of !="" in sql queries because like many, I wasn't aware that having null values as default in database tables can lead to far simpler queries.

    Doesn't it make testing for empty strings a bit more explicit?

    Tests for emptiness and being set or not.

    PHP Code:
    if( $str ) {

    // this passes as true when it contains "" or 0 or null

    if( $str === null ) { 
    if( isset( 
    $str ) )  { 

    // you know it has not been touched since it was initialized
    // ie it is not from a form fiels as, say, an empty string 
    I quite like the rigidity of initializing as null.

    Are there any more views on initializing a var as null in PHP?

    Another observation:

    Quote Originally Posted by Wardrop
    Even then, I'll go for single quotes and break into doubles to do the line break (ie. 'This is some text'."\n").
    Isn't that what PHP_EOL is for?

    echo 'This is some text'. PHP_EOL ;

    An os independent means of writing an End Of Line ?

  16. #66
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    PHP Code:
    if( $str ) {

    // this passes as true when it contains "" or 0 or null

    if( $str === null ) { 
    if( isset( 
    $str ) )  { 

    // you know it has not been touched since it was initialized
    // ie it is not from a form fiels as, say, an empty string 
    That is wrong. If $str is 0 it will fail. Else boolean algebra is completely wrong.

    But for which string I use to do huge bits of code.

    I never got into herdoc i looked at it one day thought it was cool for the day then i found templating and never questioned how i would output large amounts of code, www.smarty.net, answered that question for me.

    But for quotes
    I started using " cause as any good c programmer knows ' is for chars only not c strings.

    Then I read some where that ' this faster. ANd was like OMG i need to be .00000001 seconds faster on avg lets change from " to '. and I thought wow I just wasted 30 minutes to save 1 second.

    Then came the theory to display html in code like this
    PHP Code:
    <?php
    //Some Random code
    ?>
    <input type="<?= $type ?>">
    <?php
    // more php
    ?>
    I realized that was ugly hard to read and annoying and said templating here i come.
    Now how do i do small strings.

    It's all a really mish mash of what button i hit first. And for some styling habbit i always keep indexes $my_array['you_suck'] vs $m_array["you_suck"].

  17. #67
    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)
    Quote Originally Posted by baileylo View Post
    That is wrong. If $str is 0 it will fail. Else boolean algebra is completely wrong.
    I think the point of Cups' post was that if $str is 0, the first if statement will not differentiate between 0 and null, whereas the other two do. Using ($str === null) or (isset($str)) are ways of differentiating between a value of zero and null (no value).
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog · Twitter · Contact me
    Neon Javascript Framework · Jokes · Android stuff

  18. #68
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh cause i was goign to say

    var_dump(0 || null || "") is false

  19. #69
    SitePoint Addict GeertDD's Avatar
    Join Date
    Feb 2005
    Location
    Belgium
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mmj View Post
    Usually this is the case, but if you need to match against a pattern which contains a lot of apostrophes, isn't it easier to include the string in double quotes?
    I prefer to be consistent and never ever use double quotes around regex. Moreover, my text editor clearly marks an escaped single quote, so that helps too.

  20. #70
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Haven't touched PHP in a meaningful way in years, but when I did touch it in a meaningful way, I was a "single quote and use sprintf to make it pretty."

  21. #71
    SitePoint Zealot Dorsey's Avatar
    Join Date
    Feb 2004
    Location
    NJ
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's my vote: I stick with single quotes to indicate a string that is not interpreted (does not contain a variable), and double quotes when I want it interpreted.

    In my editor (jEdit), the two strings have different colors, so it's easier to spot an error, but mostly, applying this convention consistently improves readability. I only use concatenation if it's unavoidable for the same reason.

  22. #72
    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'll second the above.
    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
  •