Re: Quotes vs. Single Quote

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Aug 5, 2010 at 7:10 PM, Rick Dwyer <rpdwyer@xxxxxxxxxxxxx> 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.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux