colap Posted July 11, 2011 Share Posted July 11, 2011 Which is better to use echo 'Hello world' or echo "Hello world" ? Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted July 11, 2011 Share Posted July 11, 2011 well in the case of echoing a simple string, it wouldn't really matter, however if you were to echo a string with variables inside, you would want to use double quotes so you will not need to concatenate...variables inside of double quotes are parsed as a variable as opposed to a variable placed inside of single quotes (apostrophes) which would be translated as a string...for example $var = "Hello"; print("$var World"); //would print Hello World print('$var World');// would print $var World Quote Link to comment Share on other sites More sharing options...
freelance84 Posted July 11, 2011 Share Posted July 11, 2011 I started a thread once on here about optimizing php scripts. When possbile use single marks $var = 'hello world '.$name; I cant remember exactly why... im gonna try and find the post Quote Link to comment Share on other sites More sharing options...
colap Posted July 11, 2011 Author Share Posted July 11, 2011 Thinking all of cases which is better? Quote Link to comment Share on other sites More sharing options...
$php_mysql$ Posted July 11, 2011 Share Posted July 11, 2011 'Hello world' is Quote Link to comment Share on other sites More sharing options...
premiso Posted July 11, 2011 Share Posted July 11, 2011 For the sake of argument, echo is faster than print, by a margin. Single quotes, will of course be faster than double quotes. Why? Because single quotes do not have to parse string data inside of them. That being said, the difference is marginal that you would not know the difference in the end. So it really comes down to preference. I prefer to use the single quotes for echo'ing, as when I echo it is generally with HTML and I prefer not to mess with escaping quotes if at all possible. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted July 11, 2011 Share Posted July 11, 2011 using print there was simply for example purposes, I normally use echo...I prefer to use double quotes when I am working with variables simply because I believe that concatenating all the time is a pain... But really the differences are minimal and boil down to personal choice Quote Link to comment Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 I tend to use echo ""; because I like to include variables. (echo "blah {$value}") When I work with HTML at all, I usually escape PHP all together. (?><div class="blah">Blah<?php echo $value;?></div><?php) Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted July 11, 2011 Share Posted July 11, 2011 I tend to use echo ""; because I like to include variables. (echo "blah {$value}") When I work with HTML at all, I usually escape PHP all together. (?><div class="blah">Blah<?php echo $value;?></div><?php) just to nit pick a little... your line echo "blah {$value}" should be echo "blah $value" complex syntax is not needed unless text immediately follows the variable names... Quote Link to comment Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 AyKay47, It may not be needed, but it helps prevent human error and is easier to read in my opinion with {}'s. Quote Link to comment Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 To answer the actual question of performance: <?php ?><div style="height: 20px; border: 1px solid #000000; width: 100%; overflow: auto;"><?php $benchMarkTest=microtime(); for ($a=0; $a<500; $a++) { echo 'This is my string.'; } $microtime=microtime()-$benchMarkTest; ?></div><br /><?php echo $microtime; ?><div style="height: 20px; border: 1px solid #000000; width: 100%; overflow: auto;"><?php $benchMarkTest=microtime(); for ($a=0; $a<500; $a++) { echo "This is my string."; } $microtime=microtime()-$benchMarkTest; ?></div><br /><?php echo $microtime; ?><div style="height: 20px; border: 1px solid #000000; width: 100%; overflow: auto;"><?php $string="string"; $benchMarkTest=microtime(); for ($a=0; $a<500; $a++) { echo "This is my {$string}"; } $microtime=microtime()-$benchMarkTest; ?></div><br /><?php echo $microtime; ?><div style="height: 20px; border: 1px solid #000000; width: 100%; overflow: auto;"><?php $string="string"; $benchMarkTest=microtime(); for ($a=0; $a<500; $a++) { echo "This is my $string"; } $microtime=microtime()-$benchMarkTest; ?></div><br /><?php echo $microtime; ?> You can run that code anywhere and get a ratio comparison of each outcome. These are my results: 8.3E-5 7.1000000000002E-5 0.00014700000000001 0.00014700000000001 (Respectively) Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted July 12, 2011 Share Posted July 12, 2011 It's interesting to see in those tests that the first test with single quotes took longer than the double quotes. (knowing that double quotes is supposedly using the parser) My test results were this: first run 0.000709 0.000308 0.000279 0.000278 multiple runs afterwards, all very similar and average the 500 range for all, so not much a difference at all. 0.000486 0.000401 0.000505 0.000569 Quote Link to comment Share on other sites More sharing options...
teynon Posted July 12, 2011 Share Posted July 12, 2011 QuickOldCar, e-5 (-5) as in add 4 zeros. Single quotes was faster. Quote Link to comment Share on other sites More sharing options...
teynon Posted July 12, 2011 Share Posted July 12, 2011 You do make a valid point though, that "" without a variable in it is consistently slightly faster than single quotes. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 12, 2011 Share Posted July 12, 2011 The OPs question was about which is "better" to use - not the most "efficient". Saving milliseconds on a script does not outweigh the possible benefits of using a different method. My preferences are ones which I feel give me the most flexibility and allow me to have a consistent coding style. I prefer to use the single quotes for echo'ing, as when I echo it is generally with HTML and I prefer not to mess with escaping quotes if at all possible. I agree with Permiso, but I will qualify that. When doing the actual echo I too am doing it within the presentation layer (i.e. the HTML output). But, at that point I am usually only echoing variables I built within the logic of the page (i.e. the core PHP code). When building the variables for output I typically use double quoted strings because of the ability to include variables and escaped characters (e.g. linefeed, carriage return, tab stops, etc.). Also, I almost always enclose my variables within curly braces, as teynon showed, to have a consistent coding style - and I also think it makes the code more readable. If you don't, you end up not including them when you need them and spend too much time trying to debug the error. A consistent coding style and a style which is easy to interpret will help prevent simple errors and make edits/updates much easier. If a program meets it's intended purpose it is not wrong, but there are certainly instances where a particular implementation is a piece of cr*p. In the case of use single quotes or double quotes (or the heredoc or nowdoc methods), neither method has any significant benefits outside of the benefits that you get out of it. By that I mean pick a style that makes sense to you that you can be consistent with. It wouldn't hurt to read the manual on the four different methods of string syntax. There's some really good information in there: http://php.net/manual/en/language.types.string.php Quote Link to comment Share on other sites More sharing options...
teynon Posted July 12, 2011 Share Posted July 12, 2011 In addition to mjdamato, that's another reason I shy away from single quotes... Not that i'm knocking any of your guys preferences. I often times write sentences that have 's in them (The sentence above has one actually.) This would cause an error with single quotes. So I actually rarely use them. Unless it's an array ($blah['foo']) but that's a completely different story and no longer echo. Quote Link to comment 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.