I find myself utilising the double quotes often in my code.
I recently found out that everything inside double qoutes forces the interpreter to examine those contents to see if there are any variables and if it finds any, substitute them with their assigned values.. as opposed to single quotes, which just tells PHP to output whatever is in the single quotes without searching for variables.

If done frquently throughout your document, it makes the interpreter work harder then necessary, right? I am listing three versions of code below..

Version 1:

echo "<div class="div_thumbnailIllustBG">"; // using only double quotes...

Does this mean the enterpreter has to not only enterpret everything within the echo quotes, but also in the class quotes (sort of a double whammy)? In either case, this is bad coding practice, is it not?

So would this next version be more efficient?
Version 2:

echo "<div class='div_thumbnailIllustBG'>"; // using only double quotes for echo...single quotes for class...

This version is my what I am presently using (and sometimes, I also use version 1)
I assume then that the interpreter still evaluates everything inside the echo quotes for any variables right?.. but does this version mean I have shaved off some work for the interpreter by not including additional double quotes for the class aspect?

Version 3:

echo '<div class=\'div_thumbnailIllustBG\'>';

Am I right in understanding that this version would not involve the interpreter checking for any variables at all? Would that last version be the 'best' way to skim off any work from the interpreter in that respect?

Finally, is it worth going through all my code and changing as much as needed to single quotes? (keeping double quotes for variable interpretation).

Your thoughts and input is appreciated.

Cheers,

NRG

    hm..
    We had a thread here a few days ago, dealing with optimizing code. I think you need to think about where the gains are to be had. Parsing youd code in itself will not be the bulk of the time spent processing. Optimizing a queyr will change the speed a lot more.

    Add to that... Although "" will allow variables being parsed, I prefer to place variables outside the quotes, as in syntax highlighting all your vars will light up like a christmas tree:

    echo "My name is ".$name.".";
    

    Facilitating deburgging & readability

      leatherback wrote:

      hm..
      We had a thread here a few days ago, dealing with optimizing code. I think you need to think about where the gains are to be had. Parsing youd code in itself will not be the bulk of the time spent processing. Optimizing a queyr will change the speed a lot more.

      Add to that... Although "" will allow variables being parsed, I prefer to place variables outside the quotes, as in syntax highlighting all your vars will light up like a christmas tree:

      echo "My name is ".$name.".";
      

      Facilitating deburgging & readability

      I am aware of the method you demonstrated.. but to take your example as a further example..your echo statement is in double quotes.. this would mean that the interpreter must check throughout your double quotes for string parsing, yes?

      Is it not better (or for a better term, 'worthwhile') to write it as:

      echo 'My name is ' . $name . '.';
      

      My concern is not with such a single instance..as I'm sure this alone doesn't have much weight for processing.. but what I am rather curious about is when an XHTML document is littered with qouble quotes all over the place..(in the PHP tags that is) how much of a gain is it to convert the non essential double quotes into single ones?

      Also, still curious to know if nested double quotes as in my orginal posted example (version 1) is 'extra' work for the interpreter.

      Cheers,

      NRG

        The nested double quotes in your first example would not work, and in fact should throw a parse error. You would have to escape the literal quotes:

        echo "<div class=\"div_thumbnailIllustBG\">";
        

        As to whether parsing those back-slashes is "more work" than echo "<div class='div_thumbnailIllustBG'>"; (or inverting the double/single quotes in that example) is not something I expect I'll ever need to care about.

        Many examples abound on the web of testing single quotes versus double quotes for execution speed. Every one I've seen has come up with it being a non-issue if there are no variables within the string to be interpolated. It does appear that concatenating variables so that they are not within the quoted string literals (whether double- or single-quoted) is a little faster than interpolating variables within quotes (which of course does not apply to single quotes). Even then, unless you are outputting a few hundred variables within a couple thousand lines of text, the time difference is so small as to be lost in the total time needed for your request to reach the server, the process to be fired off to execute your page, any database queries and file read/write operations, and then sending the output back through the network. (In fact, even with a few hundred variables in a few thousand lines of text, I doubt if you still could tell a difference amidst the general noise of the overall page request/response cycle.)

        So long story short: unless you are writing web apps for a really, really, really busy site, it's not worth worrying about - go for whichever method makes it easiest for you to read and maintain your code. And if those micro-seconds (or more likely nanoseconds?) truly are important, chances are you still can save more time by refactoring other aspects of your application and system than you'll ever save by changing print "Hello, $world"; to echo 'Hello, ' . $world;.

          Thanks for the heads up, Nog (and thanks for pointing out my error of not escaping my double quotes in verison 1 too!).

          So in the end, I suppose I am simply splitting hairs on this issue.

          Cheers,

          NRG

            The relative savings vary from version to version of PHP, too; so even if it's "worthwhile" (on the order of nanoseconds) to go one way, that saving might disappear with the next upgrade.

            What's more, in some cases - depending on the length of the strings and the number of variables involved - having interpolated variables of the form "hello $world" can actually prove faster (again, though, still on the order of nanoseconds) than 'hello '.$world.

            The only way to tell what would really happen would be to experiment - but in this case the time spent experimenting is far larger than the time saved (though I suppose you wouldn't know that until you've done the experiment - bit of a dilemma, that).

              ok, fair enough. Thanks for the input guys..

              I think I'll just stick to Version 2 format for my pages and not care about any sliver of performance gained / lost. I suppose life is too short for this sort of thing.

              Cheers,

              NRG

                Write a Reply...