PHP includes two basic statements to output text to the web browser: echo and print. Both the echo and print statements should be coded between the opening and
closing PHP code block tags and the PHP code block can be inserted anywhere in the HTML 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 sections. 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 HTML document.
<title>A Web Page</title>
echo "This Is a basic PHP document";
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. Whitespaces and new line characters that are created in the
HTML editor by using carriage returns, spaces, and tabs are ignored by the PHP engine.
In the example below, the HTML paragraph tag is included inside of the PHP echo statement. In PHP, HTML tags (e.g., <p>>, <div>, <span>, and so on) can be used with the
print and echo statements to format output content. In these cases, the HTML tags should be surrounded by double quotes ("") so that the
PHP interpreter does not try to interpret the tags and consider them as a part of the output string.
echo <p>This is paragraph 1.</p>;
echo <p> This Is paragraph 2.</p>;
Without the use of the HTML 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.