Table of Contents

Echo vs. Print

The most common way to output text with PHP is using echo or print. In this section we will see the similarities and the differences between them.

Similarities

They are both language constructs (not functions) so they can be used without parentheses:

<?php
echo 'foo';
//foo
print 'bar';
//bar?>

They can be used to output multiple lines:

<?php
echo 'This is the first line.
This is the second line.';
//This is the first line.
This is the second line.
print 'As you can see,
it works with print too.';
//As you can see,
it works with print, too.?>

You can concatenate multiple strings using dots (because the dot is an operator that works with string):

Actually, there is no real reason to prefer print over echo (unless if you want the "1" returned by print — and you probably won't need it). As we saw, echo can do all the things that print does, moreover it allows you to use commas instead than dots.

echo is also slightly faster and shorter than print, even if these difference are almost irrelevant.

Single vs. Double quotation marks

In PHP there are two main ways to specify a string: single quotes ('foo') and double quotes ("bar").
There's also a third way — heredoc — but, in this article, we won't talk about it.

Single quotes

When you need to output a plain string, the single quotes are probably the best idea.

Variables and escaped characters (e.g. \n, \t, \" etc.) will not be expanded, except for \' and \\ (you can also write just a single \ to output the backslash). This will make the parsing of a single quoted string slightly faster than a double quoted one, and you don't have to escape double quotes (e.g. in HTML attributes) as we can see in the following examples:

<?php
echo 'This is a plain string';
//This is a plain string
$var = 123;
echo 'This $var and this \n newline character will not be expanded.';
//This $var and this \n newline character will not be expanded.
echo 'The \' single quote and the \\ backslash will be expanded.
The single \ backslash works too.';
//The ' single quote and the \ backslash will be expanded.
The single \ backslash works too.
echo '<img src="foo.jpg" alt="test" height="100" width="100">';
//<img src="foo.jpg" alt="test" height="100" width="100">?>

Double quotes

If you use a double quoted string, variables and escaped characters will be expanded.

<?php
$var = 123;
echo "This $var will be expanded. You can also use \$var if you want to avoid it.";
//This 123 will be expanded. You can also use $var if you want to avoid it.
echo "This characters will be expanded too:\nfoo\n\tbar\nbaz";
//This characters will be expanded too:
foo
bar
baz
echo "<img src=\"foo.jpg\" alt=\"test\" height=\"100\" width=\"100\">";
//<img src="foo.jpg" alt="test" height="100" width="100">
echo "You can also print characters in octal and hexadecimal notation like \141 and \x62.";
//You can also print characters in octal and hexadecimal notation like a and b.?>

Whenever is possible is better to use single quotes and avoid to include variables inside the strings. Just in few cases — when you have lot of variables that have to be included in a string — the use of double quotes may improve the readability of the code and thus it could be used.

Dots vs. Commas

As we saw in the previous paragraphs, you can use both dots and commas to output strings and variables using echo. Instead, with print, you can only use dots. So, what is the difference between them?
When using dots, all the parts are concatenated to form a single string that will be printed, while with commas, all the parts are printed one by one, without any concatenation.

Using commas is slightly faster than using dots and the output will be exactly the same (no spaces will be added between the arguments as it happens in Python) so there's no real reason to use dots with echo.