' (single quote) vs " (double quote)

I’m wondering if experienced programmers among you can tell me which is better to use between ’ and ". When I started, I read somewhere that the choice is mainly up to my own personal style/taste, and it really doesn’t make any difference between ’ and ", so I made a choice to use double quote.

But now, I’m taking a baby step towards learning PHP and MySQL (so that I may be able to get my Corona apps to talk to database), and I read we can place simple variables inside a double-quoted string. Single-quoted string will literally send what’s inside the quote, while double-quoted string will be evaluated first and the result of the evaluation would be spat out. (Confusing, eh?)

So, I thought, if I want to be extra clear as to my intention, I thought I should just go with single-quote (it will spit out literally what’s inside the quote), rather than taking a chance with double-quote, which may, if coded without care, could spit out unintended result.

But then, what confuses me is this book (“PHP and MySQL Web Development” by Luke Welling and Laura Thomson) seems to use both single quote and double quote. Sample code for html uses double quote, while sample code for php uses single quote. Why? Why? Why?

I mean, I can replace all double quotes from the html sample code with single quotes, and it works just fine. (But if you swap double quotes to single quotes from php code, it can easily cause a problem because of the evaluation thingy mentioned above.) So… why didn’t they, the authors, choose single quote consistently for both html and php?

Is it advisable to use double quote for html while single quote is better for php? Is there a hidden wisdom that I should be aware of?

Naomi

Edit: And… now I see sample php codes that mix up double and single quotes (and they don’t even need to use double quotes because no evaluation/interpolation are required in these samples.) Maybe there’s no magic rule. Maybe it’s just a result of more than one person writing the code with different style/taste. I just don’t know… but I suppose I just have to forge ahead and establish my own rules. *sigh* [import]uid: 67217 topic_id: 29138 reply_id: 329138[/import]

I tend to use double quote for all my strings unless I have a need to embed a doubled-quoted string or don’t want variables expanded (in PHP). If you use double quotes for all your strings in PHP you will find that you end up expanding a “variable” by mistake, but I find that I make lots of those mistakes when I’m switching between languages (Lua, PHP, JavaScript, C). (After programming in Lua I find myself starting my PHP or C comments with “–”.)

In PHP, double quoted strings allow for variable expansion and also supports string escapes (e.g., “\n”), which I use a lot when generating HTML and CSS code.

Every programming language has it’s quirks and differences and it’s something you have to deal with. When I pick up a new language I find it helpful to create a “cheat sheet” showing the language quirks and differences that I can refer to quickly.

Good luck with your new adventures.

-Tom [import]uid: 7559 topic_id: 29138 reply_id: 117194[/import]

Thank you, Tom, for sharing your thoughts & experience. I think I’m going to stick with double quotes when working in Lua/Corona-SDK, and will consider using double quotes for html, but will use single quotes consistently in PHP scripts (unless I find it too convenient not to use double-quoted string with variables inside – and that would be an exception that I would make on case-by-case basis.)

Hopefully, I can live with my rule without regrets later on.

Thanks again.

Naomi [import]uid: 67217 topic_id: 29138 reply_id: 117197[/import]

I’m somewhat inconsistent myself. The best rule is to use the one that avoids escaping characters. So if you have a " in your string, use ’ and vice-versa.

The second rule of thumb is to use the easiest to type. I’ve heard of some keyboards where certain characters are much harder to type.

As for language crossing, I doubt one will ever be happy.
In C, single quote and double mean different things and are used in different contexts.

What you described in PHP also applies to shell (e.g. bash) and Perl. But Lua has [[]] for what they do with single quotes.

I’m currently staring at a bunch of XML I need to parse and all the quotes are single quoted.

[import]uid: 7563 topic_id: 29138 reply_id: 117240[/import]

Thank you, Eric @ewing, for sharing your thoughts & experience too. I only know Corona implementation of Lua, and am now just looking into PHP (and truth be told, I’m not too familiar with html either.) I can’t imagine how confusing things can get with loads more language in your head.

Good news is, I feel like I’m making a good progress. I’m beginning to see why I may want to switch between ’ and " – especially, as you mentioned, when switching would help avoid escaping characters.

Having worked on my first game for a year, it’s amazing how PHP seems not so foreign – hopefully, I can get my 2nd game (which is pretty much done as far as the basic single player mode is concerned) completed without another year of learning this new world of web service thing. All I want is to implement a simple mechanics after all.

Thanks again.

Naomi [import]uid: 67217 topic_id: 29138 reply_id: 117242[/import]