How to Display Content in a Web Page with PHP - dummies

How to Display Content in a Web Page with PHP

By Steve Suehring, Janet Valade

You display content on your web page with PHP echo or print statements; they both do the same thing. An echo or print statement produces output, which is sent to the user’s browser. In fact, the browser handles the output as HTML.

The general format of an echo statement is

echo outputitem,outputitem,outputitem,...

where the following rules apply:

  • An outputitem can be a number, a string, or a variable. A string must be enclosed in quotes.

  • List as many outputitems as you need, separated by commas.

echo Statements
echo Statement Output
echo “Hello”; Hello
echo 123; 123
echo “Hello”,”World!”; HelloWorld!
echo Hello World!; Not valid; results in an error message
echo “Hello World!”; Hello World!
echo ‘Hello World!’; Hello World!

The echo and print statements output a line of text that’s sent to a browser. The browser considers the text to be HTML and handles it that way. Therefore, you need to make sure that your output is valid HTML code that describes the web page that you want the user to see.

When you want to display a web page (or part of a web page) by using PHP, you need to consider three parts involved in producing the web page:

  • The PHP script: PHP statements that you write.

  • The HTML source code: The source code for the web page that you see when you choose View→Source in your browser. The source code is the output from the echo or print statements.

  • The web page: The web page that your users see. The web page results from the HTML source code.

The echo or print statements send exactly what you echo to the browser — no more, no less. If you don’t echo any HTML tags, none are sent.

PHP allows some special characters that format output, but they aren’t HTML tags. The PHP special characters affect only the output from the echo or print statement — not the display on the web page. For instance, if you want to start a new line in the PHP output or the HTML source code, you must include a special character (n) that tells PHP to start a new line.

However, this special character just starts a new line in the output; it does not send an actual HTML tag to start a new line on the resulting web page.

Stages of Web Page Delivery
echo Statement HTML Source Code Web Page Display
echo “Hello World!”; Hello World! Hello World!
echo “Hello World!”;
echo “Here I am!”;
Hello World!Here I am! Hello World!Here I am!
echo “Hello World!n”;
echo “Here I am!”;
Hello World!
Here I am
Hello World! Here I am!
echo “Hello World!”;
echo “<br />”;
echo “Here I am!”;
Hello World!<br />Here I am!” Hello World!
Here I am!
echo “Hello”;
echo ” World!<br />n”;
echo “Here I am!”;
Hello World!<br />
Here I am!”
Hello World!
Here I am!

To look at the differences between the stages in creating a web page with PHP more closely, consider the following two print statements:

print "Line 1";
print "Line 2";

If you put these lines in a script, you might expect the web page to display this:

Line 1
Line 2

However, this is not the output that you would get. The web page would display this:

Line 1Line 2

If you look at the source code for the web page, you see exactly what is sent to the browser, which is this:

Line 1Line 2

Notice that the line that is sent to the browser contains exactly the characters that you printed — no more, no less. The character strings that you printed didn’t contain any spaces, so no spaces appear between the lines. Also notice that the two lines are printed on the same line.

If you want a new line to start, you have to send a signal indicating the start of a new line. To signal that a new line starts here in PHP, print the special character n. Change the print statements to the following:

print "line 1n";
print "line 2";

Now you get what you want, right? Well, no. Now you see the following on the web page:

line 1 line 2

If you look at the source code, you see this:

line 1
line 2

So, the n did its job: It started a new line in the output. However, HTML displays the output on the web page as one line. If you want HTML to display two lines, you must use a tag, such as the <br /> tag. So, change the PHP end-of-line special character to an HTML tag, as follows:

print "line 1<br />";
print "line 2";

Now you see what you want on the web page:

line 1
line 2

If you look at the source code for this output, you see this:

line 1<br />line 2

Use n liberally. Otherwise, your HTML source code will have some really long lines. For instance, if you print a long form, the whole thing might be one long line in the source code, even though it looks fine in the web page. Use n to break the HTML source code into reasonable lines. It’s much easier to examine and troubleshoot the source code if it isn’t a mile-long line.