Jump to content

Which is better to use echo 'Hello world' or echo "Hello world" ?


colap

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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... ;D

But really the differences are minimal and boil down to personal choice

Link to comment
Share on other sites

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... :P

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.