difference between ' and "?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Ambush Commander wrote:But it's annoying when you're writing a sentence.

Code: Select all

$string = 'But it's annoying when you're writing a sentence.';
Parse error! ARGH!
LOL! True that! Guess it depends on the situation.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

$string = 'But it\'s annoying when you\'re trying to write a sentence!';
:p (same problem with double quotes .. )

I use single quotes, doubles if I need a variable parsed inline.

Using doubles unecessarily (i.e. on a literal/static string) is less efficient than single quotes.

Why?

Single quotes says to PHP: "Here, take this value as it is."

Doubles say: "Read through me first, and check if there are any variables that need parsing and replacing."

IMO singles are better than doubles :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Maybe the ultimate quote is a backslash... almost never used, and when used, well, you'd have to escape it anyway. Don't know how the lexer would make sense of it though.

Edit - as to your reply, ideally, we wouldn't have to escape anything at all. I hate writing a string, firing up PHP, discovering a parse error, then remembering that I had forgotten to escape my quotes. It's really annoying.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

use blocks then :P

Code: Select all

$str = <<<BLOCK
BOO!
BLOCK;
You can change BLOCK for any phrase/char you like.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I've just heard heredoc is clunky, slow and interpolates variables...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

aye, they do.

I don't use them as they make reading code terrible.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Jenk:

have you benchmarked the actual difference in time between single and double quotes?


I just did, it's about .000004 slower to use doubles quotes to assign an average string with one variable in it....

it would only be noticeable if it was done 250 thousand times or so
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

regardless of how small the difference, that still shows it is more efficient to use single quotes :)
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

As much as an optimization nut as I am, I still regard this as code standardizarion issue. Just be consistant.

Can't believe this topic got two pages! :lol: WTG Method Man!
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Buddha443556 wrote:As much as an optimization nut as I am, I still regard this as code standardizarion issue. Just be consistant.

Can't believe this topic got two pages! :lol: WTG Method Man!
lol, yeah indeed. I tend to not really bother too much with double or single quotes, it's usually single quotes. I find enough else in my code to optimise, simplifying logic being the number #1 thing. It's always amazing how much you can optimise when you refactor your code after a couple of months.
Post Reply