Quote usage in the source code

I've noticed that quote usage seems to be arbitrary in ZP's source code. For example, in full-image.php this line:

`header("Location: " . getAlbumFolder(FULLWEBPATH) . pathurlencode($_zp_current_album->name) . "/" . rawurlencode($_zp_current_image->name));`

Uses double quotes while the case statements further down use single quotes. I don't know if you guys realize this or not, but PHP evaluates everything inside double quotes while treating everything inside single quotes as a string literal.

Like in the patch attached to #407. I've made use of PHP's double quote processing to insert the value of `$image` with out having to concatenate. But in the actual construction of `$image`, I use single quotes and concatenation for ease of reading.

I guess what I'm getting at is this. Is there a preference to quote usage in the source code?

Comments

  • acrylian Administrator, Developer
    I really don't know. I personally prefer double quotes and including variables with ".$string.", because I can spot them easier that way.
    Sometimes I use the single quotes within double quotes because it does sometime look a little cleaner to me than escaping with backslashes. Well, maybe we really should have a code formatting guide.

    The only place I know of where we use them really by default is for the default values of function variables because phpdoc made some parsing problems with double quotes.
  • The important thing to realize here is that there is a performance difference between using single and double quotes. PHP must evaluate double quotes, searching for strings within the quotes that it needs to process.

    Simply, if a string does not have anything inside of it that needs to be processed ($variables, etc.), don't enclose it in double quotes. You're only causing yourself a longer load time and a more intensive process for the server.

    Even more strictly speaking, it is best (for the machine, perhaps not so much for the human coder) to always concatenate variables into strings.

    As for ZP source code, from what I've seen (this is taking into account the hacks that myself and others I work with have coded into my own ZP install), there's a loose preference for using single quotes, and only using double quotes when appropriate.
  • As a rule I try to use single quotes for "constants" and double quotes for strings, but often you will find that the use of quotes has more to do with what is in the strings. Sometimes single quotes are used to avoid having to use \" for double quotes.

    I don't think there is a coding preference here. (Let's not get too anal!)
  • I'm not trying to get too anal. I just want to make sure I know how the project is being run. I prefer using single quotes for every literal string and double quotes when the string should be evaluated (e.g. if there are escaped sequences in the string). If it doesn't make much difference, then I will use quotes as I am used to using them.

    I was also trying to raise the point that zebaron picked up on. Double quotes require execution time whenever they are encountered.
  • There is one standard we need to follow regarding quotes.

    If you are using a quoted string in a function definition for the default value, it needs to be in single quotes. Our document extractor replaces double quotes with " in the document which looks terrible.
  • trisweb Administrator
    zebaron is correct, we use single quotes for literals as often as possible, and double-quotes when needed (to evaluate internal expressions) or even when not needed just because. This is a recommendation, not a strict rule.

    We are (or I am, at least) very aware of the behavior of these two quoting types, along with the so-called performance difference.

    The logic in the loose rules is that the performance difference between double- and single-quotes is negligible, so worrying too much about it is simply nitpicking and we shouldn't waste time on it. See: http://spindrop.us/2007/03/03/php-double-versus-single-quotes/

    More specifically, the fastest possible thing is single-quotes with concatenation, double-quotes with concatenation is nearly exactly the same, and the slowest is double-quotes with inline variables.

    So really, the rule that makes the most sense is screw worrying what kind of quotes you use, but only use inline-variables in double quotes when necessary for readability when building long value-ridden strings.

    Still, even remembering that it doesn't matter *much*, try to use single quotes for string literals anyway, because, well, why not? And we might shave 0.0001 seconds off each page load. Whoop-de-doo!
  • Works for me.
Sign In or Register to comment.