1.5. Displaying Content in a Web Page

You display content on your Web page with echo statements. An echo statement produces output, which is sent to the user's browser. 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 (using variables is discussed in the section "Using PHP Variables," later in this chapter. A string must be enclosed in quotes.

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

Table 1-1 shows some echo statements and their output.

Table 1.1. echo Statements
echo StatementOutput
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!

echo 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 stages in producing the Web page:

  • The PHP script: PHP echo statements that you write.

  • The HTML source code: The source code for the Web page that you see when you choose ViewSource in your browser. The source code is the output from the echo statements.

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

NOTE

The echo 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 statement — not the display on the Web page. For instance, if you want to start a new line in the PHP output, you must include a special character ( ) 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 HTML tag to start a new line on the Web page. Table 1-2 shows examples of the three stages.

Table 1.2. Stages of Web Page Delivery
echo StatementHTML Source CodeWeb 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! "; echo "Here I am!";Hello World!Here I amHello 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 /> ";echo "Here I am!";Hello World!<br />Here I am!"Hello World!Here I am!

Table 1-2 summarizes the differences between the stages in creating a Web page with PHP. To look at these differences more closely, consider the following two echo statements:

echo "Line 1";
echo "Line 2";

If you put these lines in a script, you might expect the Web page to display

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 echoed — no more, no less. The character strings that you echoed didn't contain any spaces, so no spaces appear between the lines. Also notice that the two lines are echoed 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, echo the special character . Change the echo statements to the following:

echo "line 1
";
echo "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 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:

echo "line 1<br />";
echo "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 liberally. Otherwise, your HTML source code will have some really long lines. For instance, if you echo 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 to break the HTML source code into reasonable lines. It's much easier to examine and troubleshoot the source code if it's not a mile-long line.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.103.154