I’ve seen a lot of questions and false assumptions regarding strings and output. Here is a short review of some common questions:
print vs. echo
I often see people suggesting that others should use echo as opposed to print for performance reasons. While it is true that echo is faster than print, the difference is insignificant. The reason echo is faster is because print behaves like a function (even though it’s a language construct) and sets a return value. This being the case, there are some important differences between the two, and there are a few cases where you have to use print in place of echo.
As I mentioned, print behaves like a function. You can use it just as any other funciton call and it has a return value (always 1). Like print, echo is also a language contruct, but does not behave like a function. Here are some cases where print must be used in place of echo:
$b ? print "true" : print "false";
//with echo
function isOne($x)
{
if($x == 1)
{
echo $x . ' = 1';
return true;
}
else
{
echo $x . ' != 1';
return false;
}
}
//with print
function isOne($x)
{
if($x == 1)
return print($x . ' = 1');
else
return !print($x . ' != 1');
}
In the print implementation of isOne the speed difference between echo and print is negated.
Concatenated strings Vs. Multiple calls to echo
Is it faster to concatenate all of your output into a single string and then output it or is it faster to output your strings as you go? The answer is the former; concatenating strings is faster than doing output. Does this mean that you should cram all of your output into one string and output it at the end of your script? Not necessarily. Like the performance difference between print and echo, the performance difference here is almost always negligible. Use which ever method makes sense at the time. You should prefer to concatenate your output, but sometimes ‘output as you go’ makes for cleaner code which is more important in this case.
Double vs. Single quotes
PHP provides two different ways to encase strings: Double quotes and single quotes. There is an important difference between the two. If your string is encased in double quotes, you can place variables with in it like so:
$x = 5;
echo "x is equal to $x";
What this means is that PHP must parse the string, looking for any variables. Single quotes on the other hand are never parsed, meaning that if your string contains no variables it is preferable to use single quotes. Similar to the previous examples, the performance difference is minor; however, using double quotes for no apparent reason is just wasting runtime. Here are some good reasons to use double quotes:
$x = 5;
//your string contains a variable
echo "x = $x";
//your string contains single quotes
echo "Matt said, 'peanut butter is awesome'";
In the second case, it would be faster to escape your single quotes using \ but the performance difference is minor and using double quotes looks cleaner.
Output without echo (or print)
Print and echo are not the only ways to do output. At any point in your code you can close your PHP tags and start doing output as if it were normal HTML:
<?php
for($i = 0; $i < 10; $i++)
{
?>
<p>Hello</p>
<?php
}
?>
This will output Hello, nested in paragraph tags, ten times. This method is useful when you have a large amount of output that you don’t want to have to worry about nesting in quotes. Personally, when using this method, I prefer to use PHP’s alternate control flow syntax:
<?php if($x == 10) : ?>
<div>X is equal to 10</div>
<?php else : ?>
<div>X is not equal to 10</div>
<?php endif; ?>
You can use this syntax with all of PHP’s control flow statements (ie while, if, for, foreach, switch, etc…).
Output Buffering
What should you do if you have a large string that needs to be manipulated? Normally if you had a very large string, such as a block of HTML, you could use the method I just described, however, doing it like that would output the string immediately. Using quotes is clumsy and unnecessary. The answer is to use an output buffer:
<?php
ob_start(); //start the output buffer
?>
<div>My Website</div>
<p>This is my website where I write about stuff and junk. Enjoy!<p>
<div class="footer">copyright 2009 tinsology.net</div>
<?php
$output = ob_get_clean();
echo str_replace('2009', '2010', $output);
?>
In the above example, ob_start() starts the output buffer. From this point on, any output, including output from print and echo, will be stored in the buffer. ob_get_clean() returns the output as a string and deletes the buffer content. PHP’s output control functions contain some very useful functionality, I highly recommend that you read the documentation carefully: PHP output control functions