Output in PHP

This article explains how to correctly use echo and print, single and double quotation marks, dots and commas, explaining the differences and providing examples.

Table of Contents

  1. Echo vs. Print
  2. Single vs. Double quotation marks
  3. Dots vs. Commas
  4. Conclusions

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

Differences

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.

<?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
?>

Conclusions

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:

Ezio Melotti - ©2007 - This work is licensed under a Creative Commons BY-NC-SA 3.0 License.