cunoodle2 Posted March 23, 2010 Share Posted March 23, 2010 I don't know what I just cannot get about it. There are a ton of posts on this and I just cannot realize when best to use double quotes and when to use single. My questions are.. 1. Which requires less processing if you are NOT using a variable in say an echo like this.. <? //Single quotes?? echo 'Hello World'; //or double quotes?? echo "Hello World"; ?> 2. What is the best way to use a variable (for performance reasons)... <? //Single quotes?? echo 'Hello $var'; //or double quotes?? echo "Hello ".$var; ?> 3. What do you think would be the most efficient use of this code... <? while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { //how can I make this next line LESS ugly and fastest processing... echo " <option>".$row['City'].", ".$row['State']."</option>\n"; } ?> ((note the above code works fine.. just looking for processing power/speed)) Any other general rules on when to use single vs double would be greatly appreciated. Another random thing I thought of while writing this.. when I use double quotes and the "\n" it writes a blank line. Is there a way to do that with single quotes? Quote Link to comment https://forums.phpfreaks.com/topic/196199-can-someone-finally-explain-to-me-single-vs-double-quotes/ Share on other sites More sharing options...
o3d Posted March 23, 2010 Share Posted March 23, 2010 To answer your questions: "1. Which requires less processing if you are NOT using a variable in say an echo like this.." The php pre-compiler has become so advanced that you wouldn't notice a speed difference between the single and double quotes. However in the past it was well known to try steer clear of double quotes (or smart quotes). Single quotes were seen as not-so-clever quotes. "2. What is the best way to use a variable (for performance reasons)..." For simplicity sake I would personally go for echo "hello $var"; instead of echo "hello".$var;. It just reads better imo. Again performance wise there is not much difference. "3. What do you think would be the most efficient use of this code..." echo " <option>$row['City'], $row['State']</option>\n"; And No, you cannot write a line-break with single quotes as it is seen as 'dumb' quotes. Opinions on these topics may differ but in principle use quotes in context. Quote Link to comment https://forums.phpfreaks.com/topic/196199-can-someone-finally-explain-to-me-single-vs-double-quotes/#findComment-1030324 Share on other sites More sharing options...
ignace Posted March 23, 2010 Share Posted March 23, 2010 echo '<option>',$row['City'],', ',$row['State'],'</option>', "\n"; Notice the , instead of . which is faster then concatenating the string I second o3d altough I still use single-quotes for my normal strings and double-quotes whenever variables are involved to make it better readable (thus not really a performance issue but more a readability issue) Quote Link to comment https://forums.phpfreaks.com/topic/196199-can-someone-finally-explain-to-me-single-vs-double-quotes/#findComment-1030400 Share on other sites More sharing options...
oni-kun Posted March 23, 2010 Share Posted March 23, 2010 echo '<option>',$row['City'],', ',$row['State'],'</option>', "\n"; Notice the , instead of . which is faster then concatenating the string Do you want to bet? Here is a random test I wrote awhile back: http://zwap.to/0021K It's the second test, it's quite slow comparitively on this kind of concatenation. Echo is a structure, It's pointless to use a structure on an unstructured string. Quote Link to comment https://forums.phpfreaks.com/topic/196199-can-someone-finally-explain-to-me-single-vs-double-quotes/#findComment-1030415 Share on other sites More sharing options...
ignace Posted March 23, 2010 Share Posted March 23, 2010 http://www.electrictoolbox.com/php-echo-commas-vs-concatenation/ echo $string1, $string2, $string3; Approx average 2.08 seconds echo $string1 . $string2 . $string3; Approx average 3.48 seconds I have done some searching on the web and it greatly differs some say it's faster others say it's slower Sources: http://hungred.com/useful-information/php-micro-optimization-tips/ http://www.phpbench.com/ http://www.simplemachines.org/community/index.php?topic=47171.0 Quote Link to comment https://forums.phpfreaks.com/topic/196199-can-someone-finally-explain-to-me-single-vs-double-quotes/#findComment-1030661 Share on other sites More sharing options...
Catfish Posted March 23, 2010 Share Posted March 23, 2010 From what I read in the PHP manual, I think they say that single quotes are not parsed by PHP and double quotes are. I gathered that means PHP will just handle strings in single quotes as they are - "no parsing". Double quotes it will swap out variable values etc. For some reason, over the years of hacking, I've ended up adopting a policy to use single quotes for strings most of the time. This was usually because of needing to use double quotes inside strings a lot of the times. Regarding readability, there's a bit of a catch 22: do you use single quotes and then have to concat variables into your string all the time: $string = 'This is a '.$value; or do you use double quotes and lose the syntax highlighting of the variable in your IDE/editor?: $string = "This is a $value"; ($value will be the same colour as the rest of the string). I ended up opting to use single quotes because of double quote horrors, and to preserve my syntax highlighting as well as giving me almost 100% consistancy through my code. See: http://www.php.net/manual/en/language.types.string.php - there is a section called 'Variable Parsing', and also talks about double and single quote string. Quote Link to comment https://forums.phpfreaks.com/topic/196199-can-someone-finally-explain-to-me-single-vs-double-quotes/#findComment-1030683 Share on other sites More sharing options...
tibberous Posted March 23, 2010 Share Posted March 23, 2010 The performance of "Hello ".$guy vs. "Hello $guy" will never made in any real-world situation. There isn't a right or wrong, so generally I just stick to what looks most readable. The one advantage of writing it as "Hello ".$guy is that, should you need to call a function on $guy you don't have change as much (eg: "Hello ".ucwords($guy)) Quote Link to comment https://forums.phpfreaks.com/topic/196199-can-someone-finally-explain-to-me-single-vs-double-quotes/#findComment-1030719 Share on other sites More sharing options...
ignace Posted March 23, 2010 Share Posted March 23, 2010 $string = "This is a $value"; ($value will be the same colour as the rest of the string). Means you should switch to NetBeans or PhpStorm Quote Link to comment https://forums.phpfreaks.com/topic/196199-can-someone-finally-explain-to-me-single-vs-double-quotes/#findComment-1030758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.