3

I am wondering the difference between the single quotes:

$this->db->select('card_code, card_color');

Versus the double quotes:

$this->db->select("card_code, card_color");

I know if I use double quotes I can place variables in the string, like so:

$this->db->select("$card_code_var, $card_color_var");

While with single quotes this does not work. And also when using it in in english or other spoken languages there can be occasions where I need to use apostrophe like here

$this->setPhrase("The cat's tail is black");

And up to here I get it.

I see on the web many official guides and documentation where they use single quotes for strings. Shouldn't be single quotes used for single characters and double quotes for strings?

$var_char = 'A';
$var_string = "A fox";

Does programmatically change something if I use a single quotes (like in the very first code above) rather than double quotes?

Mr.Web
  • 6,992
  • 8
  • 51
  • 86
  • 2
    http://php.net/types.string Single quotes are faster than double quotes because If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters – Prashant Kanse Dec 12 '15 at 07:57
  • 3
    "Shouldn't be single quotes used for single characters and double quotes for strings?" No. This isn't C++. PHP uses both types of quotes for strings. – 425nesp Dec 12 '15 at 07:57
  • 1
    @PrashantKanse Technical “faster” but practically almost negligible difference in real world use. – Giacomo1968 Dec 12 '15 at 07:59
  • I found it a practical thing to go by this rule of thumb, unless you have situations where contained quote chars dictate otherwise: for human readable strings (so text phrases, stuff to be translated) I use double quotes, for technical identifiers I use single quotes. – arkascha Dec 12 '15 at 08:17
  • @Mr.Web have you even tried figuring this out on your own before asking? You've been around for almost 3 years. You should know better. I'm even wondering why you don't have any down votes. – mrun Dec 12 '15 at 08:19
  • 1
    Tku guys. @mrun I don't see how your comment may be helpful and your question is rhetorical. If I had figured it out I would not even asked the question. Further I don't see how "time" and "years in the field" are the solution to a question. I don't "know better", the first barrier to learn something is thinking you already know everything about it. I do know a lot of stuff, but I feel I'll never stop learning, even little details do make the difference, this is the reason for my question. If is a matter of "double question" than I apologise. – Mr.Web Dec 12 '15 at 11:07
  • I meant you should know better when to ask questions on SO. It's clear that you haven't done any research whatsoever before posting the question. – mrun Dec 12 '15 at 11:11

1 Answers1

1

One useful additional fact I may mention is speed - in case of double quotes "" PHP will search every string for variables. This really is not an issue in general day to day programming, but if you have a specific large script where performance really matters, optimizing it may pay off a bit.

pavlovich
  • 1,935
  • 15
  • 20
  • 2
    According to this comment, at least, single quotes aren't necessarily always faster. http://php.net/manual/en/language.types.string.php#120160 – billrichards Aug 09 '18 at 20:29