Re: Quotes vs. Single Quote

From: Date: Fri, 06 Aug 2010 02:53:27 +0000
Subject: Re: Quotes vs. Single Quote
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-307330@lists.php.net to get a copy of this message
On Aug 5, 2010, at 10:43 PM, Michael Shadle wrote: > On Thu, Aug 5, 2010 at 7:10 PM, Rick Dwyer <rpdwyer@earthlink.net> wrote: >> Hi List. >> I've mentioned before that I am both just beginning to learn PHP AND I have inherited >> a number of pages that I'm trying to clean up the w3c validation on. >> >> Something that confuses me is how the code on the page is written where in one instance, it >> follows this: >> >> echo "<table border='1'><tr>.... >> >> And elsewhere on the page it follows: >> >> echo '<table border="1"><tr>.... >> >> In what I've read and from many of the suggestions from this board, the latter seems >> to be the better way to code, generally speaking. >> >> So given that the page has javascript in it, perhaps the reason for the previous developer >> switching between the two was for ease of incorporating JS?.... Don't really know... but what I >> would like to know is it considered poor coding switch between the two on a single page or is it >> perfectly acceptable? >> >> 2nd question, in the 3 lines below: >> >> $_SESSION['newpage'] = $newpage; >> $checkstat = "select field from table where fieldid = $field_id"; >> $result1 = @mysql_query($checkstat,$connection) or die("Couldn't execute >> query"); > > You could always do: > > $result1 = mysql_query("SELECT field FROM table WHERE fieldid = > $field_id", $connection) or die("Couldn't execute query"); > > a) I capped SQL verbs. Make it more readable :) > b) why make a variable just to throw it in the next line? > c) Make sure $field_id is truly an integer. If not, intval, > mysql_escape_string, something along those lines. Also put it in > single quotes if not an integer. > d) I left double quotes in the error, because it has a single quote > inside of it. The small micro-optimization performance you might get > is probably not worth the readability factor. > > My general rules of thumb: > > I use double quotes if: > a) I have single quotes inside the string > b) I need variables to be parsed > c) I need control characters like \n parsed > > I use single quotes always: > a) For array indexes $foo['bar'] > b) If I don't need variable parsing, control characters, etc. why not? > > You'll get a minimal performance gain by using single quotes > everywhere in PHP where you don't -need- double quotes, but that's a > micro-optimization and there's probably more important things for you > to be doing. > > For HTML, -always- use double quotes. > > <tag attribute="bar" /> is the right way. > <tag attribute='bar' /> is the wrong way. > > I'd go into more explanation but there simply doesn't need to be one. Michael: Well put.. exactly the type of instruction I was looking for. Thanks, --Rick

« previous php.general (#307330) next »