Outputting Data in PHP

May 14th, 2012

The main methods of outputting data are:

  • The echo Statement
  • The print statement
  • The print_r statement (commonly used to output arrays)

Both echo and print output data to a user’s browser.

People often confuse the print command as a function because you can use parentheses with its argument list.
The print command can return either a true or false, but echo does not.
When outputting text with quote marks, you need to “escape” them. This involves putting a \ before each quote mark (see below).

The print_r statement is used mainly to display arrays since it displays output from a variable in a readable format.


// Outputting data using the echo statement

echo "Hello from the \"new\" World";

// Outputting data using the print statement

print("Hello World");

// Outputting an array with print_r

$test = array('Orange', 'Apple', 'Kumkwat', 'Lychee');

The above will output the following:
Array ( [0] => Orange [1] => Apple [2] => Kumkwat [3] => Lychee )


In terms of speed there is really no noticable difference between the two.