Understanding PHP, HTML, and White Space


Before I get any further into the depths of using PHP to send HTML and other data to the Web browser, it's important to fully comprehend the PHP process. With PHP you can send data (a combination of HTML tags and text) to the Web browser, which will, in turn, display it as the Web page the end user sees. Thus, what you are doing with PHP is creating the HTML source of a Web page. Pictorially, this means PHP is generating the HTML in Figure 1.6 (accessed by choosing View > Source or View > Page Source in your browser), which the Web browser will turn into Figure 1.7.

Figure 1.6. You'll use PHP to generate the HTML source of a Web page (like this)…


Figure 1.7. …which the Web browser then turns into a nicely formatted display.


With this in mind, there are essentially three areas where you can affect spacing: in your PHP scripts, in your HTML source, and in the rendered Web page. The extra spaces, tabs, and blank lines you create are generically known as white space.

PHP is generally white space insensitive, meaning that you can space out your code however you want to make your scripts more legible. HTML is also generally white space insensitive. Specifically, the only white space in HTML that affects the rendered page is a single space (multiple spaces still get rendered as one). Consequently, the HTML in Figure 1.8 will make the same page (see Figure 1.7) as the HTML in Figure 1.6.

Figure 1.8. Extraneous white spacing in the HTML source will not affect the look of a page (see Figure 1.7) but can make the source easier to review.


To alter the spacing of the finished Web page, use the HTML tags <br /> (line break, <br> in older HTML standards) and <p></p> (paragraph). To alter the spacing of the HTML source created with PHP, you can

  • Use echo() or print() over the course of several lines.

    or

  • Use the newline character (\n) within double quotation marks.

I'll demonstrate both of these techniques in the following script.

To create white space

1.

Open second.php (refer to Script 1.3) in your text editor.

2.

If desired, change the page's title (Script 1.4).

 <title>White Space</title> 

Script 1.4. This script demonstrates the various types of white space created within the PHP code, the HTML source, and the rendered Web page.


3.

If desired, delete the hard-coded HTML line: <p>This is standard HTML.</p> (line 9).

4.

After the initial PHP tag, press Return or Enter.

The blank line created by the Return/Enter key will not affect the PHP script, the HTML source, or the rendered HTML page, but it will begin to make the PHP script more legible (less congested).

5.

Change the existing echo() statement so that it runs over multiple lines.

 echo 'This echo() statement runs over the course of two lines!'; 

As you'll see once this script is executed, the Return/Enter key pressed in mid-sentence (so that over begins on the next line) generates HTML source over two lines. This works because the echo() statement continues until it hits the concluding single quotation mark.

6.

Type another echo() statement that uses the line break and newline characters.

 echo "<br />This line should appear separately in the Web page.\n\n"; 

In this statement I am accomplishing two goals. First, I am sending a break character so that the Web browser displays this sentence on its own line. Second, I conclude the statement with two newlines to affect the HTML source (for the newline to work, you must use double quotation marks).

7.

Add one last echo() statement.

 echo 'Now I\'m done.'; 

Here I've used single quotation marks, but to print the single quotation mark (or apostrophe) in I'm, I have to escape the character.

8.

Save the file as whitespace.php, upload to your Web server, and test in your Web browser (Figure 1.9).

Figure 1.9. The only way to alter the spacing of a displayed Web page is to use HTML tags like <br /> (see Figure 1.10).


Figure 1.10. The PHP script creates some of this HTML code, which the browser then displays according to common formatting rules (see Figure 1.9).


You should see that the only spacing created in the final Web page by this script was accomplished by the <br />, which is printed between lines! and This line.

9.

View the HTML source of the page (Figure 1.10).

Depending upon your browser, you can achieve this by

  • Going to the View menu and choosing Source or Page Source.

    or

  • Right-clicking in the Web page and choosing View Source or View Page Source (Windows).

    or

  • Control-clicking in the Web page and choosing View Source or View Page Source (Macintosh).

In the HTML source you can see how the extra spacing in the PHP code is not transmitted. Using the newline character (refer to Script 1.4, line 14) or continuing a statement over multiple lines (lines 11 and 12) does have an effect on the HTML source but not on the rendered Web page.

Tips

  • Use white spacing liberally in your PHP scripts to make them easier to code and edit.

  • When using PHP to generate HTML, you should try to make the most readable HTML source possible, in case you need to peruse and debug the raw HTML source.

  • That being said, in the interest of saving space in the book, the scripts written herein will not be as liberal with white space as those I would write normally (or as I would recommend you write them).




    PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide
    PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    ISBN: 0321336577
    EAN: 2147483647
    Year: 2005
    Pages: 166
    Authors: Larry Ullman

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net