![]() |
Differences Between Single and Double Quotes
It may all seem a little trivial to be discussing which kinds of quotes to be using. However, there is a difference in PHP and it's important to know what those differences are. Really, it will save a lot of bills on boxes of aspirins in the long-run.
There are 3 types of quotes, but we will be looking at the 2. I will list the 3 below just so you know for future reference.
The first 2 are the types of quotes we will be concentrating on. For readability I use the single quotes for all text and the double quotes for new lines (of course unless I use PHP's constant, PHP_EOL). The new line and carriage return characters (\n and \r) will not be resolved inside single quotes and will be displayed literally. They MUST be encapsulated in double quotes for them to appear as new lines. Take the following examples as a good reference to the difference between the 2: Single Quotes PHP Code:
Double Quotes PHP Code:
As you can see the former outputs the variable as it is, not resolving its value of Buddha, whilst the latter does. Personally, I always use the single quotes unless I need to output any variables or new line characters. I've always been brought up to never encapsulate PHP variables in any types of quotes and therefore I always concatenate them like so: PHP Code:
PHP Code:
|
I do it the
Quote:
|
Yea, I do agree that it makes sense to use double quotes with new lines. However, I do it like so for new lines because of the way I was brought up with PHP:
PHP Code:
|
Yeah. I started out using Single quotes, so thats what I'll continue to use. It just seems neater to me. I like having the variables separate from the text.
|
...And the parsers love it, too!
|
I've alwys used double quotes, because it's easier, you can just put the vars in there.
But now that I think about it, escaping every double quote of the html takes alot of time, and I might actually swith :p |
Single quotes ftw.
Single quotes are actually a bit faster when you've got a string and a variable, or so I've been led to believe. |
Quote:
And tbh, you shouldn't EVER need to escape quotes in a document as it should never be used with PHP and instead in a templating file.... |
oh well, let's say you have a function that generates the newest articles or something. I use li's for that kind of stuff...and if you want to style it, adding a class would be nice, so you get
<li class="topArticle"><a href="$articleurl">$articlename</a></li> you need to do this kind of things with or without templating file no? That's 4 double quotes that need to be escaped, and I have using single quotes in my html (i think it's even invalid for xhtml). I could be wrong though :p |
I think what Bluesaga means is not doing:
PHP Code:
PHP Code:
|
He said templating file, so I assumed he was talking about a file with the layout, and where each part is called as a fnction or something.
But yea, I guess that's an option ... |
Erm sled, that can still be in a templating file :)
PHP Code:
|
The performance difference between the two types of quotes is rather interesting.
Consider the following PHP Code samples: v1.php: PHP Code:
PHP Code:
PHP Code:
The benchmarks were run on a P2 400MHz with 192MB RAM. The linux "time" command was used to calculate the actual CPU time used by each script. The results are as follows: # time php v1.php real 0m1.260s user 0m1.224s sys 0m0.032s # time php v2.php real 0m1.271s user 0m1.224s sys 0m0.036s # time php v3.php real 0m1.973s user 0m1.944s sys 0m0.032s Notice how the v1.php and v2.php timings are very similar, it is interesting that using double quotes without embedded variables is actually faster than using single quotes. The marked difference is when it comes to using an embedded variable in v3.php. Note how the execution time increases by 0.7s, a 50% increase in execution time. From this, I conclude that:
|
What bluesaga meant with the first part of his msg, was that instead of doing this:
PHP Code:
PHP Code:
But personally, I wouldn't use ", because it looks much sloppier, and it's more difficult to read the variables, etc.. I only use " in mysql querys: PHP Code:
However, on a more serious note, you should never use a query like that. Instead you should use sprintf. |
PHP Code:
|
Please read the following article by Wildhoney:
http://www.talkphp.com/showthread.php?t=1062 It will explain everything to you why you should ALWAYS use sprintf. |
Quote:
|
Yea, but in most cases you don't have a specific value like that.
And in most "common" script, you run a check in the database where it searches for something that matches the input of the $_POST value. And when you're dealing with those cases, you should always use sprintf. |
Not really, as long as you filter the $_POST first with for example, strip_tags, and mysql_escape_string etc.... then it should be fine.
|
Did you read Wildhoney's article? ...
Anyways, what do I care :P You can do whatever querys you want. I prefer to do it the safest way there is. |
| All times are GMT. The time now is 09:29 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0