From: Rick Dwyer on
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");


If I were to recode in the latter style, should they not look like this:

$_SESSION['newpage'] = $newpage;
$checkstat = 'select field from table where fieldid = "'.$field_id.'"';
$result1 = @mysql_query($checkstat,$connection) or die('Couldn\'t execute query');


The focus being here:

"'.$field_id.'"';
('Couldn\'t execute query')

Is this correct?

Thanks for the help.

--Rick


From: Josh Kehn on

On Aug 5, 2010, at 10:10 PM, Rick Dwyer 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");
>
>
> If I were to recode in the latter style, should they not look like this:
>
> $_SESSION['newpage'] = $newpage;
> $checkstat = 'select field from table where fieldid = "'.$field_id.'"';
> $result1 = @mysql_query($checkstat,$connection) or die('Couldn\'t execute query');
>
>
> The focus being here:
>
> "'.$field_id.'"';
> ('Couldn\'t execute query')
>
> Is this correct?
>
> Thanks for the help.
>
> --Rick
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
Rick-

It is generally accepted that you should use single quotes whenever possible. I only use double quotes when writing SQL queries (so I don't have to continually escape them for the single quotes) and when I need to output control characters like "\r" or "\n".

It would be considered "best practice" to make consistent use of them, but it wouldn't be something I would loose sleep over.

Regards,

-Josh
From: Michael Shadle on
On Thu, Aug 5, 2010 at 7:10 PM, Rick Dwyer <rpdwyer(a)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.
From: Rick Dwyer on

On Aug 5, 2010, at 10:43 PM, Michael Shadle wrote:

> On Thu, Aug 5, 2010 at 7:10 PM, Rick Dwyer <rpdwyer(a)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






From: Paul M Foster on
On Thu, Aug 05, 2010 at 10:10:26PM -0400, Rick Dwyer 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?
>

Not acceptable and sloppy. Be consistent in your coding style. In
general, HTML attributes should be surrounded by double quotes. I don't
know about javascript. Moreover, it's generally better to simply output
HTML rather than to echo it, like:

<table border="1"><tr>
<td>
<?php echo $some_value; ?>
</td>
</tr>

> 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");
>
>
> If I were to recode in the latter style, should they not look like this:
>
> $_SESSION['newpage'] = $newpage;
> $checkstat = 'select field from table where fieldid = "'.$field_id.'"';
> $result1 = @mysql_query($checkstat,$connection) or die('Couldn\'t execute query');
>

This is a matter of taste, but I've heard that if you can do it without
string concatenation, it executes faster. In my opinion, the former is
better because it's easier to follow than the second, where you have
strings concatenated with single and double quotes all over the place.

Paul

--
Paul M. Foster