Results 1 to 5 of 5

Thread: [RESOLVED] Single Quote vs Double Quote, What to default to?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Resolved [RESOLVED] Single Quote vs Double Quote, What to default to?

    A single quote or string literal can be used in PHP to declare a String, ignoring all variables names inside. It may also contains a double quote inside of it. A double quote on the other hand - still declaring a String - accepts variable names to be passed inside, preventing the need for concatenation. A single quote also does not parse escape sequences such as \n and \r. Is there a performance difference between the two types of String Literals? Why not use a double quote all the time and simply escape other double quotes if they are required in the String. Besides outputting variable names I don't see the point in having two strings.

    Personally I mostly use double quotes as my default choice when creating Strings unless I need to display a lot of text that contains quotations. In the latter case if there isn't much text I will escape the double quotations but otherwise I would just use single quotes.

    I was just wondering if there was a reason (besides displaying variable names) for PHP to have multiple methods to declare a String.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Single Quote vs Double Quote, What to default to?

    Of course there is a performance difference... you pointed it out yourself: "single quote also does not parse escape sequences such as \n and \r." -- since single quotes don't allow the parsing of \n or of variable names, it can be treated as literal text and doesn't need to be run through any further processing. I work from the opposite opinion... I use single quotes, unless I'm inserting $vars right into my text. And even then I'm not above .concatenating.my.strings.together either.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Single Quote vs Double Quote, What to default to?

    Outside of an extensive loop where a string is being echo'd is the performance difference that noticable?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Single Quote vs Double Quote, What to default to?

    meh... probably not... but I sleep better at nights

    in the end it probably comes down to preference... as I mentioned I tend to use the single quote... probably to prevent me from doing something VBish by accident... :P

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Single Quote vs Double Quote, What to default to?

    thanks for the clarification

Posting Permissions

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



Click Here to Expand Forum to Full Width