|
-
Sep 2nd, 2011, 07:52 AM
#1
Thread Starter
Frenzied Member
[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.
-
Sep 2nd, 2011, 08:01 AM
#2
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
-
Sep 2nd, 2011, 08:57 AM
#3
Thread Starter
Frenzied Member
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?
-
Sep 2nd, 2011, 09:32 AM
#4
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
-
Sep 2nd, 2011, 02:30 PM
#5
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|