Re: [PHP] Quotes vs. Single Quote
On 6 August 2010 16:18, Bill Guion wrote: > At 8:31 AM -0400 08/06/10, tedd wrote: > >> Cheers, >> >> tedd >> >> PS: Considering that this is Friday. I have a grammar question for the >> group. I said above: >> >> "neither CSS, PHP, or any web language exist in a vacuum." >> >> Is the word "neither" appropriate in this sentence? >> >> Normally, two items can be compared by "neither" or "nor", but what about >> more than two items? Is it appropriate to use "neither" or "nor" for more >> than two items? > > Somewhere along the line, probably in college (if it were before college, it > would have been so long ago I would have forgotten it), a professor said to > handle this sort of thing thusly: > > neither A, nor B, nor C > > A little more wordy, but completely unambiguous. "neither CSS, PHP, nor any web language exist in a vacuum." would probably do. All negatives, so little wiggle room really. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
At 8:31 AM -0400 08/06/10, tedd wrote: Cheers, tedd PS: Considering that this is Friday. I have a grammar question for the group. I said above: "neither CSS, PHP, or any web language exist in a vacuum." Is the word "neither" appropriate in this sentence? Normally, two items can be compared by "neither" or "nor", but what about more than two items? Is it appropriate to use "neither" or "nor" for more than two items? Somewhere along the line, probably in college (if it were before college, it would have been so long ago I would have forgotten it), a professor said to handle this sort of thing thusly: neither A, nor B, nor C A little more wordy, but completely unambiguous. -= Bill =- -- Don't find fault. Find a remedy. - Henry Ford -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
At 9:09 AM -0400 8/6/10, Andrew Ballard wrote: On Fri, Aug 6, 2010 at 8:31 AM, tedd wrote: While it may not be obvious, the statement: > is flawed (IMO). The "best" way to handle this is to define a class (or id) for the table in a css file and then set the border (i.e., styling) to whatever you want. For example, your HTML would look like: And your CSS would contain: .my_table { border: 1px solid black; } I more or less agree with you, but sometimes it's technically a little more difficult than that. -snip- As is often the case with CSS, that's a good bit more text to accomplish the same effect as an older, smaller attribute. :-) Andrew Andrew: The problem you cite is well said and your point is well taken. However, the main point I am making is to move this problem totally out of the HTML/PHP arena and place it where it belongs, which is inside CSS -- after it *is* a presentation problem. IMO, it is *far* better to deal with browser comparability problems from one CSS file than it is to sort through all your PHP files looking for the phrase . From my experience, when you have a problem, it is always better to give it a name and deal with it from one location. As for "older, smaller attributes", they are only getting older and their importance lessens with time (I can relate.) :-) Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
On Fri, Aug 6, 2010 at 8:31 AM, tedd wrote: > While it may not be obvious, the statement: > > > > is flawed (IMO). > > The "best" way to handle this is to define a class (or id) for the table in > a css file and then set the border (i.e., styling) to whatever you want. For > example, your HTML would look like: > > > > And your CSS would contain: > > .my_table > { > border: 1px solid black; > } > I more or less agree with you, but sometimes it's technically a little more difficult than that. The border attribute on the table tag affects not only the table itself, but also the cells inside it. The CSS attribute only draws a border around the table. I believe the CSS equivalent of how most browsers (I tested Fx 3.6.8, IE 7, Google Chrome 5, Opera 10.53, and Safari (Windows) 5.0.1) render takes a little more: table.my_table, table.my_table > thead > tr > th, table.my_table > tbody > tr > th, table.my_table > tfoot > tr > th, table.my_table > thead > tr > td, table.my_table > tbody > tr > td, table.my_table > tfoot > tr > td { border: solid 1px black; } And, of the browsers listed above, IE7 did not render the table correctly. (I'm guessing it must not properly handle the child CSS selectors.) If you do it without the child selectors: table.my_table, table.my_table th, table.my_table td { border: solid 1px black; } All the browsers render it the same, but it has the side effect that cells in nested tables also inherit the borders unless you do something to exclude them: table.my_table, table.my_table th, table.my_table td { border: solid 1px black; } table.my_table table, table.my_table table th, table.my_table table td { border: none; } As is often the case with CSS, that's a good bit more text to accomplish the same effect as an older, smaller attribute. :-) Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
On 6 August 2010 13:31, tedd wrote: >I have a grammar question for the > group. I said above: > > "neither CSS, PHP, or any web language exist in a vacuum." > > Is the word "neither" appropriate in this sentence? > > Normally, two items can be compared by "neither" or "nor", but what about > more than two items? Is it appropriate to use "neither" or "nor" for more > than two items? > http://en.wikipedia.org/wiki/Neither says that "either" can be used for many items if they are in a list (like you've used), so neither would probably follow the same argument. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
On Aug 6, 2010, at 8:08 AM, tedd wrote: > At 10:10 PM -0400 8/5/10, Rick Dwyer wrote: >> 2nd question, in the 3 [2] lines below: >> >> $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: >> >> $checkstat = 'select field from table where fieldid = "'.$field_id.'"'; >> $result1 = @mysql_query($checkstat,$connection) or die('Couldn\'t execute >> query'); > > Rick: > > Others gave you good advice on quotes, but I'll address your second question > on database queries. > > The following is in the form of what I normally do: > > $query = "SELECT field FROM table WHERE field_id = '$field_id' "; > $result = mysql_query($query) or die("Couldn't execute query"); > > Please note these are my preferences (others may have different preferences): > > 1. I use UPPERCASE for all MySQL syntax. > > 2. I do not use the @ before mysql_query because that suppresses errors. I > prefer to see errors and fix them. > > 3. It's not necessary to include the second argument (i.e., $connection) in > mysql_query. > > 4. IMO, a query should be named $query and a result should be named $result. > If I have several results, then I use $result1, $result2, $result3, and so on. > > 5. I try to match MySQL field names to PHP variable names, such as field_id = > '$field_id'. This makes it easier for me to read and debug. > > 6. Also note that the PHP variable $field_id is enclosed in single quotes > within the query. > > 7. For sake of readability, in the query I also place a space after the last > single quote and before the ending double quote, such as field_id = > '$field_id' ". -- I do not like, nor is it readable, to have a singledouble > quote (i.e., '"). > > There is one additional thing that I do, but it requires an included > function. For your kind review, in my query I do this: > > $result = mysql_query($query) or die(report($query,__LINE__,__FILE__))); > > and the report function I include to the script is: > > // show dB errors == > > function report($query, $line, $file) > { > echo($query . '' .$line . '' . $file . '' . mysql_error()); > } > ?> > > That way, if something goes wrong, the report function will show in what file > and at what line number the error occurred. Now, this is OK for development, > but for production you should comment out the echo so you don't report errors > publicly. Besides, you should have all the errors fixed before your script > becomes production anyway, right? :-) > > HTH, > > tedd > Tedd, Well said! I pretty much follow those same standards as well. Especially with the naming of variables to match field names. I also make sure that any form field names match my database names. It makes updating and inserting records so much easier! I've written a database class that allows me to update and insert records as easily as this: $db->insert("table_name",$_POST); $db->update("table_name","id_field_name",$id,$_POST); And, yes, I do sanitize the data to make sure it doesn't do bad things to my database! :) Take care, Floyd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
At 9:05 PM -0700 8/5/10, Michael Shadle wrote: Leave the single quotes for parameters, indexes, code, not attributes - $.02 Agreed. "Render unto Caesar (HTML) the things that are Caesar's and unto God (PHP -- Lord forgive me) the things that are God's." In other words, when writing code in another language use the syntax that is appropriate for that language Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
At 11:00 PM -0400 8/5/10, Paul M Foster wrote: On Thu, Aug 05, 2010 at 10:10:26PM -0400, Rick Dwyer wrote: > echo " And elsewhere on the page it follows: > echo ' 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: Rick: I agree with Paul. I would only add that you should use what languages best serve your needs. While it may not be obvious, the statement: is flawed (IMO). The "best" way to handle this is to define a class (or id) for the table in a css file and then set the border (i.e., styling) to whatever you want. For example, your HTML would look like: And your CSS would contain: .my_table { border: 1px solid black; } That way at some future date, you may want to change the border color, size, whatever and it's a trivial thing to do so without having to search through all your code to find ill-placed styling attributes. As I always say, neither CSS, PHP, or any web language exist in a vacuum. It always best to use whatever language that makes your life (and others) simpler. Cheers, tedd PS: Considering that this is Friday. I have a grammar question for the group. I said above: "neither CSS, PHP, or any web language exist in a vacuum." Is the word "neither" appropriate in this sentence? Normally, two items can be compared by "neither" or "nor", but what about more than two items? Is it appropriate to use "neither" or "nor" for more than two items? -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
At 10:10 PM -0400 8/5/10, Rick Dwyer wrote: 2nd question, in the 3 [2] lines below: $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: $checkstat = 'select field from table where fieldid = "'.$field_id.'"'; $result1 = @mysql_query($checkstat,$connection) or die('Couldn\'t execute query'); Rick: Others gave you good advice on quotes, but I'll address your second question on database queries. The following is in the form of what I normally do: $query = "SELECT field FROM table WHERE field_id = '$field_id' "; $result = mysql_query($query) or die("Couldn't execute query"); Please note these are my preferences (others may have different preferences): 1. I use UPPERCASE for all MySQL syntax. 2. I do not use the @ before mysql_query because that suppresses errors. I prefer to see errors and fix them. 3. It's not necessary to include the second argument (i.e., $connection) in mysql_query. 4. IMO, a query should be named $query and a result should be named $result. If I have several results, then I use $result1, $result2, $result3, and so on. 5. I try to match MySQL field names to PHP variable names, such as field_id = '$field_id'. This makes it easier for me to read and debug. 6. Also note that the PHP variable $field_id is enclosed in single quotes within the query. 7. For sake of readability, in the query I also place a space after the last single quote and before the ending double quote, such as field_id = '$field_id' ". -- I do not like, nor is it readable, to have a singledouble quote (i.e., '"). There is one additional thing that I do, but it requires an included function. For your kind review, in my query I do this: $result = mysql_query($query) or die(report($query,__LINE__,__FILE__))); and the report function I include to the script is: ' .$line . '' . $file . '' . mysql_error()); } ?> That way, if something goes wrong, the report function will show in what file and at what line number the error occurred. Now, this is OK for development, but for production you should comment out the echo so you don't report errors publicly. Besides, you should have all the errors fixed before your script becomes production anyway, right? :-) HTH, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
On 6 August 2010 07:34, Peter Lind wrote: > On 6 August 2010 04:10, 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 " >> >> And elsewhere on the page it follows: >> >> echo ' >> >> 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. >> > > It isn't better or worse. The only thing that makes a difference is > what suits you - stick to what works for you. Both double-quotes and > single-quotes can result in gotchas (in double quotes you have to > escape more, which you have to keep in mind, whereas in single quotes > you have a lot less power, which you might forget). There's no > difference in performance, which leaves just one thing: personal > preference. > > Regards > Peter > > -- > > WWW: http://plphp.dk / http://plind.dk > LinkedIn: http://www.linkedin.com/in/plind > BeWelcome/Couchsurfing: Fake51 > Twitter: http://twitter.com/kafe15 > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > You also have heredoc ... 'A "daft" div. Click me and you\'re a numpty.'); echo << All In One The div below should say that it is a "daft" div and if you click it then you're a numpty. {$array['value']} END_HTML_WITH_EMBEDDED_JS; ?> will output ... All In One A "daft" div. Click me and you're a numpty. The above example shows how escaping can be minimized. I've done it manually, but it could have been done by using htmlentities() or htmlspecialchars() with ENT_QUOTES. Only the JS code needed the escaping. The \" because the " is in an attribute value (which used " as the delimiter) and the \' because the ' is used as a string delimiter for the alert() call. Obviously, it IS a bit of a mess. Using normal string concatenation, it becomes a lot harder. 'A "daft" div. Click me and you\'re a numpty.'); echo " All In One The div below should say that it is a \"daft\" div and if you click it then you're a numpty. {$array['value']} "; ?> So, 3 \. The first \ is to escape the second \, the third to escape the ". Which results in \" which is an escape of the " in the HTML. Now imagine the above string was a search and replace via some regular expression. Sure you _can_ work it out, but sometimes you just keep adding \ until it works. You may need upto 6 \ in a row... or more! Richard. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
On 6 August 2010 04:10, 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 " > > And elsewhere on the page it follows: > > echo ' > > 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. > It isn't better or worse. The only thing that makes a difference is what suits you - stick to what works for you. Both double-quotes and single-quotes can result in gotchas (in double quotes you have to escape more, which you have to keep in mind, whereas in single quotes you have a lot less power, which you might forget). There's no difference in performance, which leaves just one thing: personal preference. Regards Peter -- WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
On Thu, Aug 5, 2010 at 8:51 PM, Adam Richardson wrote: > Tim Bray, who knows a little bit about XML dialects (tongue in cheek), > appears to default to the single quote as his delimiter of choice: > http://www.tbray.org/ongoing/ Side note, looks like his stuff is auto-generated by something, so it's defined once and replicated many times for templating... but also I do see some attributes with double quotes mixed in, i.e.: I work for Google, but the opinions expressed here are my own, and no other party necessarily agrees with them. A full disclosure of my professional interests is on the author page. Contributions Comment feed for ongoing: Serif · Sans-Serif I should say also - double quotes helps when using inline JavaScript in attributes too :) add that to my reasons. I just default to double quotes because of history developing things, it just works easier. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
On Thu, Aug 5, 2010 at 8:51 PM, Adam Richardson wrote: > I would suggest that saying is "the wrong way" is a > rather strong assessment. Whether you're talking about SGML (the > grandparent), XML (the parent), or XHTML, the use of a single quote is > perfectly valid, and has served a purpose since inception. If I'm crafting > markup and embedding something that has a double quote within an attribute > (often times an alt attribute on an image), I don't hesitate to use the > single quote as the attribute delimiter. That said, it's often easier if > you standardize on one, and most choose to use double quotes the default > delimiter. > That said, if there are some sources to point to that make a case for the > deprecation of single quotes in (X)HTML attributes, please let me know. Well, most people use htmlspecialchars() to encode text for safe display to a browser. By default, it only encodes double quotes: http://php.net/htmlspecialchars "The default mode, ENT_COMPAT, is the backwards compatible mode which only translates the double-quote character and leaves the single-quote untranslated." We've run into issues where we thought our forms were fairly secure, but some people decided to echo "" type stuff, which works fine if you encapsulate attributes in double quotes, but in single quotes, we found out that anyone who had a single quote in that value would break the page. Now, I typically use a central wrapper function for encoding and decoding, and if it was in use there, sure, I could have thrown in ENT_QUOTES and solved that issue. However, the vast majority of everything uses double quotes, and there is not really a reason to NOT use them. Of course, I put it out there like that to simply push it because it should be appropriate for everyone. You are right though - it WILL work with single quotes (as we can see), but I recommend a single way of doing things to keep things consistent, and it has been the unspoken standard everywhere I've ever looked for markup... (Funny enough, that page has an example with a single quoted attribute) Leave the single quotes for parameters, indexes, code, not attributes - $.02 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
On Thu, Aug 5, 2010 at 10:53 PM, Rick Dwyer wrote: > > On Aug 5, 2010, at 10:43 PM, Michael Shadle wrote: > > > > > For HTML, -always- use double quotes. > > > > is the right way. > > is the wrong way. > > > > I'd go into more explanation but there simply doesn't need to be one. > I would suggest that saying is "the wrong way" is a rather strong assessment. Whether you're talking about SGML (the grandparent), XML (the parent), or XHTML, the use of a single quote is perfectly valid, and has served a purpose since inception. If I'm crafting markup and embedding something that has a double quote within an attribute (often times an alt attribute on an image), I don't hesitate to use the single quote as the attribute delimiter. That said, it's often easier if you standardize on one, and most choose to use double quotes the default delimiter. Tim Bray, who knows a little bit about XML dialects (tongue in cheek), appears to default to the single quote as his delimiter of choice: http://www.tbray.org/ongoing/ Now, speaking to questions/concerns about javascript events frequent use of single quotes beg the question: Why are you embedding javascript events into the markup of the page? I'm aware of many sources that advocate against mixing javascript and html in this way (see the books PPK on Javascript, DOM Scripting, etc.) That said, if there are some sources to point to that make a case for the deprecation of single quotes in (X)HTML attributes, please let me know. Adam -- Nephtali: PHP web framework that functions beautifully http://nephtaliproject.com
Re: [PHP] Quotes vs. Single Quote
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 " > > And elsewhere on the page it follows: > > echo ' > > 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: > 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
On Aug 5, 2010, at 10:43 PM, Michael Shadle wrote: > On Thu, Aug 5, 2010 at 7: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 " >> >> And elsewhere on the page it follows: >> >> echo ' >> >> 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. > > is the right way. > 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Quotes vs. Single Quote
On Thu, Aug 5, 2010 at 7: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 " > > And elsewhere on the page it follows: > > echo ' > > 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. is the right way. 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
Re: [PHP] Quotes vs. Single Quote
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 " > > And elsewhere on the page it follows: > > echo ' > > 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Quotes vs. Single Quote
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 " And elsewhere on the page it follows: echo ' 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