Aug 10

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 by all means drop the parentheses:
    <?php
        echo 'foo';
    
        print 'bar';
    ?>
  • They can be used to output multiple lines:
    <?php
        echo 'This is the first line.
        This is the second line.';
    
        print 'As you can see,
        it works with print, too.';
    ?>

Differences

  • print returns 1 so it can be used as a function:
    <?php
        (1 == 1) ? print 'true' : print 'false'
        //Outputs 'true'
    
        (1 == 1) ? echo 'true' : echo 'false'
        //Doesn't work
    
        echo (1 == 1) ? 'true' : 'false'
        //Outputs 'true'
    ?>
  • With echo you can “concatenate” with a comma.  I put concatenate in quotes because it looks and feels like concatenation is happening, but it’s not actually concatenation.  I’ll talk about that more later.
    <?php
        echo 'With echo you can ', 'use the comma ', 'to output multiple parameters.';
        //Works
    
        print 'With print ', 'you will get ', 'an error.';
        //Doesn't work
    ?>

Realistically there is no real reason to prefer print over echo unless you want to use it as a function.  Give me a solid case for this usage and I’ll bend, but I’ve never needed to use it.  echo can do all the things that print does better: it’s slightly faster than print…and it’s one less character to type!

Dots vs. Commas

As I mentioned earlier, you can use both dots and commas to output strings and variables using echo but with print, you can only use dots. So, what’s the difference?

When using dots, all the parts are concatenated to form a single string that will then be printed, while with commas, all the parts are printed individually, and although the end result appears to be concatenation, each item is actually output one-by-one.

This actually makes the use of commas slightly faster than using dots because the string concatenation is skipped but the end result is exactly the same – no spaces will be added between the arguments (like in Python) – so commas are the preferred method for outputting multiple string segments and variables with echo.

<?php
    $var = 'foobar';
    echo 'The value of $var is ', $var;
    //Output: The value of $var is foobar
?>

Single Quotes vs. Double Quotes

In PHP there are two main ways to specify a string: single quotes (’foo‘) and double quotes (”bar“).  There’s also a couple more ways — heredoc and nowdoc — but I don’t want to get into that can of worms here.

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';
    //Output: This is a plain string

    $var = 123;
    echo 'This $var and this \n newline character will not be expanded.';
    //Output: 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.';
    //Output: 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">';
    //Output: <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.";
    //Output: 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";
    //Output: This characters will be expanded too:
    //foo
    //    bar
    //baz

    echo "<img src=\"foo.jpg\" alt=\"test\" height=\"100\" width=\"100\">";
    //Output: <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.";
    //Output: You can also print characters in octal and hexadecimal notation like a and b.
?>

Bottom line is that it’s better to use single quotes and avoid to include variables inside the strings.  The only real time you may want to use double-quotes is when you have lot of variables that have to be included in a string.  In this case, the use of double quotes may improve the readability of the code.

Conclusion

  • Use echo instead of print
  • Don’t use parentheses with echo (or any language constructs for that matter)
  • Use single quotes if you don’t have to use escaped characters or need to expand many variables
  • Always use a comma instead of a dot when joining strings and variables in an echo statement