Tuesday, September 2, 2014

Output in PHP

12:59 PM Posted by Zayn Ali No comments

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

  • They are both language constructs (not functions) so they can be used without parentheses:
        echo 'foo';
        // foo
    
        print 'bar';
        // bar
    
  • They can be used to output multiple lines:
        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):
        echo 'The concatenation ' . 'works with ' . 'echo...';
        // The concatenation works with echo...
    
        print '...and with ' . 'print ' . 'as well.';
        // ...and with print as well.
    

Differences

  • Only print returns a value (always 1)
  • print can also be used as a function:
        ((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
    
  • Only with echo you can output multiple parameters separated by a comma (because the comma is part of the echo construct):
        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.

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:
    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 "\"test\"";
    // test

    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.

Double quotes

If you use a double quoted string, variables and escaped characters will be expanded.
    $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 "\"test\"";
    // test

    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.
    $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:
    $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:
  • Always use echo instead of print.
  • Never use parentheses with echo.
  • Prefer single quotes if you don't have to expand variables ($var) or escaped characters (like \n).
  • Avoid to include variables inside strings.
  • With echo, use commas instead of dots.
  • Use the dot to concatenate if you are not using echo.