Many articles say that double quotes are bad. Does it mean that it's recomended to use single quotes always and not even in "echo"?

CASE 1

echo 'Something';

Here I should use single quotes.

=============================
CASE 2

$tnow = date('Y-m-d H:i:s', $timestamp);
$tnow = date("Y-m-d H:i:s", $timestamp);

Single quotes or double quotes? Both of them work.

=============================

CASE 3

$do = preg_replace('/[^a-z]/', '', $do);
$do = preg_replace("/[^a-z]/", "", $do);

Single quotes or double quotes? Both of them work.

=============================

CASE 4

if(!isset($delete)) $delete='no';
if(!isset($delete)) $delete="no";

Single quotes or double quotes? Both of them work.

=============================

CASE 5

$howm=mysql_num_rows($check);
if ($howm == '1') {}

$howm=mysql_num_rows($check);
if ($howm == "1") {}

$howm=mysql_num_rows($check);
if ($howm == 1) {}

Single quotes or double quotes or no quotes?

=============================

CASE 6

if ($limit > 95) {}
if ($limit > "95") {}
if ($limit > '95') {}

Single quotes or double quotes or no quotes?

=============================

CASE 7

if ($start == "0") {}
if ($start == '0') {}

Single quotes or double quotes? Both of them work.

=============================

Thanks.

    If you are dealing with numbers (e.g., cases 5, 6 and possibly 7), then do not use quotes since you are not dealing with strings. If you are dealing with strings, use single quotes unless you need the additional escape sequences of double quotes, or if you want to use variable interpolation (the expansion of variables embedded in a string to their values).

      And the only reason to go with single quotes over double quotes, when there is no particular reason to use either for the string in question (no variable evaluation, escaping etc), is speed. And the difference is minimal.

      Moreover, I suspect that

      $str = 'string literal concatenated with' . $var;

      is less efficient than

      $str = "string literal containing some $var"

      I've not done any actual profiling on this either, but the speed difference ought to be minute.

      Other than that I've heard people claim that "string with $var inside it" is more error prone (or at least easier to overlook the fact of their being a variable inside it). But, in my opinion that comes down what editor you use as well. The one I use colour string literals green, but any variable inside it shows up in black.

      Concerning Laserlight's comment on not quoting numbers, I'd like to add "unless you for some reason are dealing with strings containing numbers".

      $numberString = "5";
      if ($numberString === "5")	// true - value is 5, and types match
      
      if ($numberString === 5)		// false. value is 5, but types don't match
      
      if ($numberString == 5)		// true. value is 5 and we don't check types
        johanafm wrote:

        And the difference is minimal.

        Really minimal. Most of the time you save is in not having to hit the shift key.

        I've not done any actual profiling on this either, but the speed difference ought to be minute.

        In fact this is the only situation where double quoted strings are slower; if there aren't any variables in the string then the two are exactly the same. But if there are variables in the string they behave differently so the question of which quote to use is moot.

        The only other difference is in what needs to be escaped in double-quoted strings; regular expressions are best single-quoted because of all the extra escape characters that would otherwise be needed.

          Thank you for your replies. Probably there is no need to change double quotes to single. I was thinking it makes a bigg difference in speed as some sources say.

          The same about eregi_replace - don't know if it makes big difference if I change it to preg_replace

            johanafm wrote:

            And the only reason to go with single quotes over double quotes, when there is no particular reason to use either for the string in question (no variable evaluation, escaping etc), is speed. And the difference is minimal.

            From what I remember, speed tests done in the past have shown that the difference between the two, where there is no variable concatenation or variable interpolation involved, is neglible. My reason would be to provide a simple rule of thumb since the lack of additional escape sequences can simplify how the string literal needs to be written. (Naturally, it would make sense to go against this rule of thumb if say, the string has many single quotes but no double quotes.)

            johanafm wrote:

            I've not done any actual profiling on this either, but the speed difference ought to be minute.

            From what I remember, speed tests in the past have shown that concatenation tends to be faster than variable interpolation, but the difference is only measurable in artificial testing. Consequently, I feel that it is silly to use such a marginal difference to justify a policy of not using variable interpolation where it improves readability of code.

            johanafm wrote:

            Concerning Laserlight's comment on not quoting numbers, I'd like to add "unless you for some reason are dealing with strings containing numbers".

            Sure, but then is already covered by the fact that they are strings, not numbers, despite the ease of type conversion 🙂

            2005 wrote:

            Probably there is no need to change double quotes to single. I was thinking it makes a bigg difference in speed as some sources say.

            There is no need, but you should develop the habit of using single quote delimited strings for regular expression patterns, not for speed, but to avoid having to escape unnecessarily.

            2005 wrote:

            The same about eregi_replace - don't know if it makes big difference if I change it to preg_replace

            You should use preg_replace. The PCRE functions such as preg_replace is (or at least is soon to be) supported as the main regular expression extension over the POSIX-based functions such as eregi_replace.

              I agree. The only reason I took it up is I've heard speed as the motivation for choosing one over the other, and the only time I worry about speed is when it matters. Else I stick with ease.

              As for ereg, it raises an error for being deprecated from PHP 5.3. Don't remember what kind, but I suppose that would be E_NOTICE.

                Write a Reply...