Displaying Content

PHP includes two basic statements to output text to the web browser: echo and print. Both the echo and print statements are coded between the opening and closing PHP code block tags and can occur anywhere in the XHTML documents.

The echo and print statements appear in the following format:

echo - used to output one or more strings.

echo "Text to be displayed"

print - used to output a string. In some cases the print statement offers greater functionality than the echo statement. These will be discussed in later tutorials. For now print can be considered an alias for echo.

print "Text to be displayed"

The following examples demonstrate the use and placement of the echo and print commands in an XHTML document.

<!DOCTYPE html PUBLIC "-//W3C//DTD/XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>A Web Page</title>
</head>
<body>

<p>

<?php 
echo "This is a basic PHP document";
?>

</p>

</body>
</html>

In most cases it is necessary to display entire paragraphs in the browser window or create line breaks when displaying content. By default, the echo and print statements do not create automatic line breaks. With both the echo and print statements, it is necessary to use a <p> or <br/> tag to create paragraphs or line breaks. White space created in the XHTML editor using carriage returns, spaces, and tabs are ignored by the by the PHP engine.

In the example below, the XHTML paragraph tag is included inside of the PHP echo statement. In PHP, XHTML tags can be used with the print and echo statements to format output. In these cases, the output should be surrounded by double quotes ("") to ensure that the browser does not literally interpret the tag and display it as part of the output string.

echo "<p>This is paragraph 1</p>"
echo "<p>This is paragraph 2</p>"

Without the use of the XHTML paragraph tag, the preceding echo statements would render the content in the following format:

This is paragraph 1 This is paragraph 2

By including the paragraph tags, the statements are displayed as two separate paragraphs.

This is paragraph 1

This is paragraph 2