[Previous] [Contents] [Next]


Outputting data with echo and print

The echo statement used in Example 2-1 and Example 2-2 is frequently used and designed to output any type of data. The print statement can be used for the same purpose. Consider some examples:

echo "Hello, world";

// print works just the same
print "Hello, world";

// numbers can be printed too
echo 123;

// So can the contents of variables
echo $outputString;

The difference between print and echo is that echo can output more than one argument:

echo "Hello, ", "world";

There is also a shortcut that can output data. The following very short script outputs the value of the variable $temp:

<?=$temp; ?>

The print and echo statements are also often seen with parentheses:

echo "hello";

// is the same as
echo ("hello");

Parentheses make no difference to the behavior of print. However, when they are used with echo, only one output parameter can be provided.

The echo and print statements can be used for most tasks and can output any combination of static strings, numbers, arrays, and other variable types discussed later in this chapter. We discuss more complex output with printf in Section 2.6 later in this section.


[Previous] [Contents] [Next]