This article explains how to correctly use echo
and print
, single and double quotation marks, dots and commas, explaining the differences and providing examples.
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.
<?php
echo 'foo';
//foo
print 'bar';
//bar
?>
<?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.
?>
<?php
echo 'The concatenation ' . 'works with ' . 'echo...';
//The concatenation works with echo...
print '...and with ' . 'print ' . 'as well.';
//...and with print as well.
?>
print
returns a value (always 1)print
can also be used as a function:
<?php
((1+1) == 2) ? print 'true' : print 'false'
//true
((1+1) == 2) ? echo 'true' : echo 'false'
//Parse error: syntax error, unexpected T_ECHO in outputinphp.php
echo ((1+1) == 2) ? 'true' : 'false'
//true
?>
echo
you can output multiple parameters separated by a comma (because the comma is part of the echo
construct):
<?php
echo 'With echo you can ', 'use the comma ', 'to output multiple parameters.';
//With echo you can use the comma to output multiple parameters.
print 'With print ', 'you will get ', 'an error.';
//Parse error: syntax error, unexpected ',' in outputinphp.php
?>
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.
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.
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">
?>
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.
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
.
<?php
$var = 123;
echo 'The value of $var is ', $var;
//The value of $var is 123
echo 'The value of $var is '.$var;
//The value of $var is 123
echo "$var * 2 = ", $var*2;
//123 * 2 = 246
echo $var*2, ' is bigger than ', $var;
//246 is bigger than 123
?>
Note that, since commas can only be used with echo
, you can't use them to concatenate strings like:
<?php
$query = 'SELECT ', $value, ' FROM ', $table, ' WHERE ', $x, ' > 10';
//Parse error: syntax error, unexpected ',' in outputinphp.php
mysql_query('SELECT ', $value, ' FROM ', $table, ' WHERE ', $x, ' > 10');
//Warning: Wrong parameter count for mysql_query() in outputinphp.php
?>
We have seen the differences between echo
and print
, single and double quotes, dots and commas. The following list summarizes all the things that we said in the article:
echo
instead of print
;echo
, use commas instead of dots;echo
.Ezio Melotti - ©2007 - This work is licensed under a Creative Commons BY-NC-SA 3.0 License.