|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
using " and '
Hi guys
i would like to ask a question, hope you guys can give me an idea about this as well.. now i have $_COOKIE["cartId"] and want the query to delete according to this cookie value.. so how can i write the query since it already have doube quotes in the cart id... do i need to change the double quote to single ? e,g: ($_POST["name"],$_COOKIE["var"] and etc) sample of my code: mysql_query("delete from cart where cookieId = "$_COOKIE['cartId']"") or die("Query failed: $query<br><br>" . mysql_error()); i tried to use double quote and single as well but parse error occured.. can you please give me an idea how actually i can make this query works in such case... i wish to know when i should use single or double quote. I read a lot of the books, they mentioned that inappropriate use of " will make parse error... for me, even single quote do have such error.. i did see some they use '".add()."' in their query to refer to a value in function, i am wondering why after added double quote, single quote is still required ??? please advise. |
|
#2
|
||||
|
||||
|
I tend to prefer using the dot operator to concatenate strings:
PHP Code:
In this example, I happen to be printing the first part of the query, with an opening single quote around the cartId value, then sticking in the value of the cartId, and then the closing quote. You're already doing this in your die() code. There are other ways of doing this, but my personal style is to always split variables out of strings and concatenate them. |
|
#3
|
|||
|
|||
|
There is quite a big difference between ' and ".
Any string between a pari of single-qoutes ( ' ) is treated as a literal string, that is php does no work with it, whare as a string between a pair of doulbe-quotes ( " ) variables are parsed within it. To explain: PHP Code:
Now many people code using the fourth example, it just seems easier, However php will take a speed hit. In small programs this may not seem like much, but if this was within a loop, or run on a slower computer, it may get quite significant, especially when you go on to write larger scripts! Remember when you want to have special characters within strings they must be escaped (by placing a backslash infront of them): \n linefeed ( newline ) \r carriage return \t horizontal tab \\ backslash \$ dollar sign \" double-quote \' single-quote ( You should note that the single quote above does not have a slash before it, this is an apparent bug in the forum, it should have a slash before it! In my example code below remember this, and insert shlashes as appropriate otherwise you will get errors when you try to run the script. ) My message is always try to use single-quotes ( ' ) when you can, and conc strings ( 'this and ' . $this ) This is probably a better way to code in most circumstances and a habit you should get used to. Therefore your code would be something like this: PHP Code:
Hope this Helps |
|
#4
|
|||
|
|||
|
I like to use the { and }... so:
PHP Code:
And yes, you should be using single quotes around your array indexes unless they are numeric.
__________________
__________________________________________________ _ Wil Moore III, MCP | Integrations Specialist | Senior Consultant Are You Listed...? | DigitallySmooth Inc. |
|
#5
|
||||
|
||||
|
Quote:
Wonder if you could explain why. Using the double quotes is an old habit that'll die really hard for me (muscle memory). Is there a signifcant difference in performance when using single quotes? Or is this just a convention? Sorry to hijack. |
|
#6
|
|||
|
|||
|
In addition, it is better to put your query strings in a variable and pass that variable to mysql_query:
PHP Code:
because now you can do some quick (but nasty) debugging PHP Code:
|
|
#7
|
|||
|
|||
|
One good reason is that you can do this:
PHP Code:
PHP Code:
|
|
#8
|
||||
|
||||
|
That makes sense. Thanks for the explanation.
|
|
#9
|
|||
|
|||
|
if i put $ sign outside of the brace like you guys do, wun it cause any error ??? if that is a legal way to do it, may i know what is the difference if i put it inside the " like " .$_COOKIE['name']. " and ${_COOKIE["cartId"]}..
it looks similar to me... please advise. |
|
#10
|
|||
|
|||
|
First issue is that this:
PHP Code:
This: PHP Code:
The difference in this method as opposed to the concatenation is just that... concatenation is a little harder to read (some may argue against that)... but basically, why concatenate when you don't have to? It's kind of like scotch taping your cloths into your closet when you can simple use a hanger. If scotch tape is all you have thats fine, but if you have some hangers available, why not use them? |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > using " and ' |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|